0

I'm trying to read values from following xml (which is previously fetched from FTP):

<?xml version="1.0" encoding="utf-8"?>
<eventdata xmlns="http://www.demoweb.net/xml/eventdata" >
<site>
<sitelink>demotesting</sitelink>
<user>15101991</user>
<measurement>
 <nodelink>012019120312064500</nodelink>
 <containername>A1</containername>
 <time>2020-04-30T11:25:35</time>
 <value type="n_v_unitvalue">0.04</value>
 <value type="n_v_unitvalue_diff">0.040</value>
</measurement>
<measurement>
 <nodelink>012019120312064501</nodelink>
 <containername>A2</containername>
 <time>2020-04-30T11:25:35</time>
 <value type="n_v_unitvalue">0.0</value>
 <value type="n_v_unitvalue_diff">-0.001</value>
</measurement>
<measurement>
 <nodelink>012019120312064502</nodelink>
 <containername>A3</containername>
 <time>2020-04-30T11:25:34</time>
 <value type="n_v_unitvalue">0.0</value>
 <value type="n_v_unitvalue_diff">0.000</value>
</measurement>
</site>
<createdate>2020-04-30T11:25:35</createdate>
</eventdata>

Before I start, file is sucessfully loaded into memory :)

As you can see root node is eventdata, and site is the node where all data is contained. So basically I need to loop all measurement nodes and get the data.

I also were struggling to get out user node.. here's what I've tried so far:

using (StreamReader xml_reader = new StreamReader(xml_response.GetResponseStream()))
{
    string xml = xml_reader.ReadToEnd();
    XmlDocument xmldoc = new XmlDocument();
    xmldoc.LoadXml(xml);

    XmlNodeList mainFileContent = xmldoc.SelectNodes("eventdata");
    // XmlNodeList mainFileContent = xmldoc.SelectNodes("eventdata/site");
    XmlElement root = xmldoc.DocumentElement;

    if (mainFileContent != null)
    {
        foreach (XmlNode node in mainFileContent)
        {
            var user = node["user"].InnerText;   
        }
    }
}

What I'm missing?

THANKS GUYS

CHEERS

0

2 Answers 2

1

Your eventdata node has own xmlns declaration, you should properly handle it using XmlNamespaceManager and select the nodes with x:eventdata/x:site XPath expression

var xmldoc = new XmlDocument();
xmldoc.LoadXml(xml);

var nsmgr = new XmlNamespaceManager(xmldoc.NameTable);
nsmgr.AddNamespace("x", xmldoc.DocumentElement.NamespaceURI);

var mainFileContent = xmldoc.SelectNodes("x:eventdata/x:site", nsmgr);
foreach (XmlNode node in mainFileContent)
{
    var user = node["user"]?.InnerText;
}
Sign up to request clarification or add additional context in comments.

2 Comments

and that's only reason why I had to use XmlNamespaceManager.. because root node has own xmlns declaration?
@Roxy'Pro yes, because your xml namespace is different from default ones, listed in remarks
1

Use below code to read measurement

   using (StreamReader xml_reader = new StreamReader(xml_response.GetResponseStream()))
   {
                string xml = xml_reader.ReadToEnd();
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.LoadXml(xml);

                var nsmgr = new XmlNamespaceManager(xmldoc.NameTable);
                nsmgr.AddNamespace("ns", "http://www.demoweb.net/xml/eventdata");
                XmlNodeList mainFileContent = xmldoc.SelectNodes("ns:eventdata/ns:site",nsmgr);
                XmlElement root = xmldoc.DocumentElement;

                if (mainFileContent != null)
                {
                    foreach (XmlNode site in mainFileContent)
                    {
                        var user = site["user"].InnerText;
                        XmlNodeList measurements = site.SelectNodes("ns:measurement", nsmgr);
                        if (measurements != null)
                        {
                            foreach (XmlNode measurement in measurements)
                            {
                                var containername = measurement["containername"].InnerText;
                                var time = measurement["time"].InnerText;
                                XmlNodeList values = measurement.SelectNodes("ns:value", nsmgr);
                                if (values != null)
                                {
                                    foreach (XmlNode value in values)
                                    {
                                        var type = value.Attributes["type"].Value;
                                        var v2 = value.InnerText;
                                    }
                                }

                            }
                        }

                    }
                }
            }

1 Comment

how to get <value type=.." below time node?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.