xml parsing error (xpath, HTMLagilitypack)

290 Views Asked by At

I am trying to parse an xml. All nodes have opening and closing tags except one node that in some lines in only has this tag: <persons/>

In most of the time it appears like this: <persons> ... </persons> I cannot get values from the xml when this node is not closing like this

Here is my code:

foreach (HtmlNode man in bm.SelectNodes(".//persons"))
 {
  //store values
 }                                     

How can I overcome this issue? Even if some nodes are like this at the start:

<persons> </persons>

if there is a tag like this in the middle of the file

<persons/>

I cannot get the remaining <persons> </persons> values from the remaining lines

1

There are 1 best solutions below

1
On BEST ANSWER

why are you using htmlnode? xmlnode would be just fine.

Or else, show more codes.

Did you step through the line? Did you encounter any error?

try this:

    internal string ParseXML()
    {
        string ppl = "";

        XmlDocument doc = new XmlDocument();
        doc.LoadXml(xmlString);
        foreach (XmlElement node in doc.SelectNodes(".//person"))
        {
            string text = node.InnerText; //or loop through its children as well
            ppl += text;
        }
        return ppl;
    }