Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从XML中选择所有数据_C#_Xml_Xsd - Fatal编程技术网

C# 从XML中选择所有数据

C# 从XML中选择所有数据,c#,xml,xsd,C#,Xml,Xsd,我有一个类似下面给出的示例的XML。我想从Apply和Value中选择信息,而不考虑NameValue Id。你知道如何实现吗 <NameValueList> <NameValue Id="LegacyB2BRCOMM"> <DSAttributeList /> <Apply>false</Apply> <Value>False</Value>

我有一个类似下面给出的示例的XML。我想从Apply和Value中选择信息,而不考虑NameValue Id。你知道如何实现吗

<NameValueList>
        <NameValue Id="LegacyB2BRCOMM">
        <DSAttributeList /> 
        <Apply>false</Apply> 
       <Value>False</Value> 
      </NameValue>

      <NameValue Id="Persist">
      <DSAttributeList /> 
      <Apply>false</Apply> 
      <Value>False</Value> 
      </NameValue>
       ....
      </NameValueList>

这取决于您使用的是哪种语言,但是您应该使用这样的xpath表达式

//NameValue
然后遍历所有返回的元素并访问apply和value元素。 在XQuery中,它将是:

for $nv in //NameValue
let $apply := $nv/Apply
let $value := $nv/Value
...
return <apply>{$apply}</apply><value>{$value}</value>
XElement xml = XElement.Parse(xmlString); //xmlString is where your xml string goes
IEnumerable<XElement> elements = from nv in xml.Descendants("NameValue")
                                 select new {Apply = nv.Element("Apply").Value, Value = nv.Element("Value").Value};
//Then you can iterate through
foreach(element in elements)
{
...
}
编辑:

由于您已将问题改为C,我还将建议除以下选项之外的另一个选项

您可以使用C中XElement的Linq to XML构造来查询XML

上述XQuery的C版本为:

for $nv in //NameValue
let $apply := $nv/Apply
let $value := $nv/Value
...
return <apply>{$apply}</apply><value>{$value}</value>
XElement xml = XElement.Parse(xmlString); //xmlString is where your xml string goes
IEnumerable<XElement> elements = from nv in xml.Descendants("NameValue")
                                 select new {Apply = nv.Element("Apply").Value, Value = nv.Element("Value").Value};
//Then you can iterate through
foreach(element in elements)
{
...
}

您可以这样做:

XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(myXml);

XmlNode xn = xdoc.SelectSingleNode("NameValueList/NameValue[@Id='LegacyB2BRCOMM']");
string apply = xn.SelectSingleNode("Apply").InnerText;
string value = xn.SelectSingleNode("Value").InnerText;
或通过以下方式进行迭代:

XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(myXml);

XmlNodeList xnl = xdoc.SelectNodes("NameValueList/NameValue");
foreach (XmlNode xn in xnl)
{
    string id = xn.Attributes["Id"].Value;
    string apply = xn.SelectSingleNode("Apply").InnerText;
    string value = xn.SelectSingleNode("Value").InnerText;
}