Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.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# 从XmlNodeList中提取节点,使用名称空间,并在多个级别上显示同一个子节点_C#_Xml - Fatal编程技术网

C# 从XmlNodeList中提取节点,使用名称空间,并在多个级别上显示同一个子节点

C# 从XmlNodeList中提取节点,使用名称空间,并在多个级别上显示同一个子节点,c#,xml,C#,Xml,我正试图提取这些子节点,但到目前为止我只是头痛 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Body> <supplyCrew xmlns=

我正试图提取这些子节点,但到目前为止我只是头痛

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
  <supplyCrew xmlns="http://site.ddf.com">
     <login>
        <login>XXXX</login>
        <password>XXXX</password>
     </login>
     <flightInformation>
        <flights>
           <item>
              <arrivalDateTime>2010-11-08T22:48:00.000Z</arrivalDateTime>
              <arrivingCity>ORD</arrivingCity>
              <crewMembers>
                 <item>
                    <employeeId>020040</employeeId>
                    <isDepositor>Y</isDepositor>
                    <isTransmitter>N</isTransmitter>
                 </item>
                 <item>
                    <employeeId>09000</employeeId>
                    <isDepositor>N</isDepositor>
                    <isTransmitter>Y</isTransmitter>
                 </item>
              </crewMembers>
           </item>
           <item>
              <arrivalDateTime>2010-11-08T20:29:00.000Z</arrivalDateTime>
              <arrivingCity>JFK</arrivingCity>
              <crewMembers>
                 <item>
                    <employeeId>0538</employeeId>
                    <isDepositor>Y</isDepositor>
                    <isTransmitter>N</isTransmitter>
                 </item>
                 <item>
                    <employeeId>097790</employeeId>
                    <isDepositor>N</isDepositor>
                    <isTransmitter>Y</isTransmitter>
                 </item>

我需要一些指导,请。

我想你会用这种方法做得更快更容易

网站上有很多例子,所以很容易找到你想要的。
希望有帮助。

使用
GetElementsByTagName(“项目”)
这里的问题是有两个级别的
item
节点-一个是
flights
的子节点,另一个是
crewMembers
的子节点

编辑既然粘贴了完整的xml,显然还涉及到一个名称空间。要处理名称空间,请使用名称空间管理器为名称空间定义别名,然后可以在xpath查询中使用别名:

var nsm = new XmlNamespaceManager(xmlDoc.NameTable);
nsm.AddNamespace("s", "http://site.ddf.com");
var elemList = xmlDoc.SelectNodes("//s:crewMembers/s:item", nsm);
foreach (var node in elemList)
{
    Debug.WriteLine(node.SelectSingleNode("s:employeeId", nsm).InnerText);
    Debug.WriteLine(node.SelectSingleNode("s:isDepositor", nsm).InnerText);
    Debug.WriteLine(node.SelectSingleNode("s:isTransmitter", nsm).InnerText);
}

您可以使用LINQ2XML实现这一点

XElement doc=XElement.Load("C:/Crew_Request_Sample.xml"); 
XNamespace e = "http://schemas.xmlsoap.org/soap/envelope/";
XNamespace s = "http://site.ddf.com";
//this would access the nodes of item->crewMembers->item and put it into an Anonymous Type

var yourList=doc.Descendants(e+"Body")
.Descendants(s+"supplyCrew")
.Descendants(s+"flightInformation")
.Descendants(s+"flights")
.Descendants(s+"item")
.Descendants(s+"crewMembers")
.Descendants(s+"item")
.Select(
x=>new
{
//Anonymous Type
employeeId=x.Element(s+"employeeId").Value,
isDepositor=x.Element(s+"isDepositor").Value,
isTransmitter=x.Element(s+"isTransmitter").Value
}
);
然后可以使用for each循环访问列表

foreach(var item in yourList)
{

Console.WriteLine(item.employeeId);
Console.WriteLine(item.isDepositor);
Console.WriteLine(item.isTransmitter);
}

我是否必须包含标记@BiancaFonseca否,您不需要添加任何根元素(因为
/
将搜索过去的根节点),但这可能意味着您的xml树不在全局命名空间中,这意味着您将需要一个XmlNameSpaceManager。@BiancaFonseca感谢更新的xml-您还需要一个namespacemanager。我已经更新了。我将如何获得然后发送到的crewMembers>或每个crewMembers>?嗨,我已经尝试按照此示例进行操作,但我无法正确获得它,请您帮助我也和。我是否必须在文档子体(“项”)之前包含它们…?@BiancaFonseca编辑您的问题,并向我们展示
XML
的完整格式。
soapenv
是一个
命名空间
。它应该与代码一起添加。显示完整的XML结构,以便我可以帮助您that@BiancaFonseca这是您的xml结构
Envelope->Body->item
foreach(var item in yourList)
{

Console.WriteLine(item.employeeId);
Console.WriteLine(item.isDepositor);
Console.WriteLine(item.isTransmitter);
}