简体   繁体   English

读取导致问题的XML节点

[英]Reading XML nodes causing issues

I have one XML string, I am trying to read that using C#, but I am not getting child nodes. 我有一个XML字符串,我试图使用C#读取它,但没有得到子节点。 I am getting entire XML as inner XML string. 我正在将整个XML作为内部XML字符串。 I am not able to read the nodes. 我无法读取节点。 Here is my XML string and my code. 这是我的XML字符串和代码。

<Filters FilterName="706337_test">
    <MemberName>Dorvil</MemberName>
    <MemberId />
    <ProviderName />
    <ProviderId>706337</ProviderId>
    <SelectedProjects>5030003</SelectedProjects>
    <CNAChartSelected>false</CNAChartSelected>
    <OldProject>false</OldProject>
</Filters>

C# code trying to read the XML nodes 尝试读取XML节点的C#代码

XmlDocument xml = new XmlDocument();
xml.LoadXml(xmlstring);
XmlNodeList xnList = xml.SelectNodes("/Filters");

I can see only one child node that too filters, I need to read MemberId, MemberName etc., how to read them? 我只能看到一个也过滤的子节点,我需要读取MemberId,MemberName等,如何读取它们?

This is because your string in SelectNodes is wrong: 这是因为您在SelectNodes中的字符串是错误的:

var xml = new XmlDocument();
xml.LoadXml(xmlstring);
var xnList1 = xml.SelectNodes("/Filters");      //list of 1 element
var xnList2 = xml.SelectNodes("/Filters/*");    //list of 7 elements
foreach (XmlNode node in xnList2)
{
    Console.WriteLine(node.OuterXml);
}

Also you can use this: 您也可以使用此:

var xElements = XElement.Parse(xmlstring).Elements();
foreach (var element in xElements)
{
    Console.WriteLine(element);
}

You need to tell the app which nodes to read.. 您需要告诉应用要读取哪些节点。

XmlDocument xml = new XmlDocument();
                    xml.LoadXml(xmlstring);

                    XmlNodeList xnList = xml.SelectNodes("/Filters");
foreach (XmlNode node in xnList)
{    
    string memberName = node["MemberName"].InnerText;
}

This lets the app know to read what is inside the MemberName node.. Do the same for the other nodes and post back your results. 这使应用程序知道要读取MemberName节点内的内容。对其他节点执行相同的操作,然后回发结果。 Debug as you go to see what you are getting out of each node. 边走边进行调试,以查看从每个节点获得的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM