无法使用C#从xml读取值/节点

我正在尝试从以下xml(先前是从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>

在我开始之前,文件已成功加载到内存中:)

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.

我也在努力摆脱用户节点..这是到目前为止我已经尝试过的方法:

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;   
        }
    }
}

I checked root element and it is really eventdata:

enter image description here I couldn't read anything from xml.. what's wrong here?

我想念的是什么?

谢谢你们

干杯

评论