Home > other >  Return an Array of string in a WCF Service
Return an Array of string in a WCF Service

Time:12-20

Hello in a C# WCF Service application i want to return an array of 5 strings in a method. The above code isn't returning any errors but when i launch the Service in debug mode it only shows the first string on the Array.

Here's the IService side :

[OperationContract]
string[] NaviresXml();

Here's the Service side :

public string[] NaviresXml()
{
    try
    {
        XMLReader x = new XMLReader(FilePath);
        return new string[] { x.ReadXmlDocument_Navires() };
    }
    catch (Exception ex)
    {

        throw new Exception(ex.Message   "\n"   ex.StackTrace);
    }
}

And the XMLReader Class :

public class XMLReader
{
    public string XmlFilePath { get; set; }
    public XMLReader(string XmlFilePath)
    {
        this.XmlFilePath = XmlFilePath;
    }
    public string ReadXmlDocument_Navires()
    {
        XmlDocument xmlDoc1 = new XmlDocument();
        xmlDoc1.Load(XmlFilePath);
        XmlNodeList itemNodes = xmlDoc1.GetElementsByTagName("Navire");
        if (itemNodes.Count > 0)
        {
            foreach (XmlElement node in itemNodes)
                return "Navire"   node.Attributes["Type"].Value   "Nom"   node.Attributes["Nom"].Value;
        }
        return null;
    }
}

When i launch the Service i can see only the first string but not the others. enter image description here

What is wrong with this code?

I've tried to do it without the XMLReader Class and put the code directly in the Service side but this didn't worked.

CodePudding user response:

Move the return statement outside your loop.

StringBuilder stringContent = new StringBuilder();
if (itemNodes.Count > 0)
   {
      foreach (XmlElement node in itemNodes)
        stringContent.Append("Navire"   node.Attributes["Type"].Value   "Nom"   node.Attributes["Nom"].Value);
   }
return stringContent.ToString();

CodePudding user response:

When a return statement is executed, function execution stops and jumps out of the executing function, even if there are other statements in the body of the function. The code after return is not executed.

So you need to put the return out of the loop like this:

 public class XMLReader
    {
        public string XmlFilePath { get; set; }
        public XMLReader(string XmlFilePath)
        {
            this.XmlFilePath = XmlFilePath;
        }
        public string ReadXmlDocument_Navires()
        {
            XmlDocument xmlDoc1 = new XmlDocument();
            xmlDoc1.Load(XmlFilePath);
            XmlNodeList itemNodes = xmlDoc1.GetElementsByTagName("Navire");
            StringBuilder res = new StringBuilder();
            if (itemNodes.Count > 0)
            {
                foreach (XmlElement node in itemNodes)
                    res.append("Navire"   node.Attributes["Type"].Value   "Nom"   node.Attributes["Nom"].Value);
            }
            return res.ToString();
        }
      return null;
    }
  • Related