C# 从 XmlNodeList 中提取节点,具有命名空间,并且同一子节点出现在多个级别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12728536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Extract nodes from XmlNodeList, with namespace, and where same child appears at multiple levels
提问by Devester
I am trying to extract those subnodes, but I had got just headache so far...
我正在尝试提取这些子节点,但到目前为止我只是头疼...
<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>
with the code I can get them, but I do not know how to select each one according to their tag name to insert them into a database.
使用代码我可以获得它们,但我不知道如何根据它们的标签名称选择每个以将它们插入到数据库中。
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("C:/Crew_Request_Sample.xml");
XmlNodeList elemList = xmlDoc.GetElementsByTagName("item");
foreach (XmlNode node in elemList)
{
Debug.WriteLine(node.InnerText);
}
I need some direction, please.
我需要一些指导,请。
采纳答案by StuartLC
The problem with using GetElementsByTagName("item")here is that there are 2 levels of itemnode - one as a child of flightsand another item as a child of crewMembers.
在GetElementsByTagName("item")这里使用的问题是有 2 个级别的item节点 - 一个作为 的子项flights,另一个项作为 的子项crewMembers。
EditNow that the full xml is pasted, it is clear that there is also a namespace involved as well. To handle namespaces, make use of a namespace manager to define aliases for the namespaces, which you can then use in the xpath queries:
编辑现在粘贴了完整的 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);
}
回答by Vinicius
I think you'll do it faster and easily using this technique
我认为使用这种技术你会更快更容易地做到这一点
There is a lot of examples in the site, so it 'll be easy to find what you want. Hope it helps.
站点中有很多示例,因此很容易找到您想要的内容。希望能帮助到你。
回答by Anirudha
You can do it using LINQ2XML..
您可以使用 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
}
);
You can then access yourList using for-each loop
然后您可以使用 for-each 循环访问 yourList
foreach(var item in yourList)
{
Console.WriteLine(item.employeeId);
Console.WriteLine(item.isDepositor);
Console.WriteLine(item.isTransmitter);
}

