查找节点递归在一个XDocument [英] Finding Nodes Recursively in an XDocument
问题描述
我想,我可以俯瞰简单的东西,但我有困难提取一个XDocument递归节点
我也与此类似XML:
<?XML版本=1.0编码=ISO-8859-1>?;
<内容>
<动作>< /运转>
<输入>
<&观测GT;
< templateId />
<代码和GT;< /代码>
< VALUE>< /值>
< entryRelationship>
<&观测GT;
< templateId />
<代码和GT;< /代码>
< VALUE>< /值>
< /观察>
< / entryRelationship>
< entryRelationship>
<&观测GT;
< templateId />
<代码和GT;< /代码>
< VALUE>< /值>
< /观察>
< / entryRelationship>
< /观察>
< /进入>
< /内容>
我以为我可以使用
$ B $得到所有三个观测节点b
的foreach(在Content.Descendants的XElement元素(观察))
ExamineObservation(元);
虽然它看起来像这只能观察时,没有孩子。我也试过.Ancestors和.DecentantNodes,却没有得到我想要的东西。
我可以很容易地写出打动了我什么,我需要一个递归方法,但如果有一个,我宁愿使用现有的方法,尤其是因为我将与XML合作相当多的几个项目。我失去了一些东西明显?
需要注意的是,上面写着观察任何一个节点,我需要从代码和价值,所以在下面的例子中,我将需要处理3观察节点。观察节点的嵌套和数量是任意的。
感谢您的任何援助。
补遗
它发生,我认为我可能不会给有关XML的足够信息。我不认为标签会有所作为,但我想我应该包括他们以防万一。下面是一个实际的消息的前几行,我试图解析。 。我做了替换一些文本...隐私
<?XML版本=1.0编码= ISO-8859-1>?;
<内容的xmlns:XSI =http://www.w3.org/2001/XMLSchema-instance>
<操作>更新和LT; /运转>
<入境的xmlns =金塔:HL7-组织:V3>
<观察classCode =OBSmoodCode =EVN>
< templateId根=.../>
<代码代码=...codeSystem =...codeSystemName =...显示名=...>
< /代码>
<值的xsi:type =...代码=...codeSystem =...codeSystemName =...显示名=...>
< /值>
< entryRelationship TYPECODE =...>
<观察classCode =...moodCode =...>
我只是跑在VS2012这个代码,并打在 Console.WriteLine()
3次,正确输出观察节点及内容:
的XElement内容= XElement.Parse(yourXmlStringWithNamespaceHeader);
的foreach(的XElement OBS中content.Descendants(观察))
Console.WriteLine(obs.ToString());
编辑 - 考虑到新的命名空间信息,并使用的XDocument
而不是的XElement
:
的XNamespace NSE =金塔:HL7-组织:V3;
的XDocument内容= XDocument.Parse(yourXmlStringWithNamespaceHeader);
的foreach(的XElement ELE在content.Descendants(NSE +观察))
Console.WriteLine(ele.ToString());
I figure I am overlooking something simple, but I am having difficulty extracting nodes from an XDocument recursively.
I have XML similar to this:
<?xml version="1.0" encoding="iso-8859-1"?>
<content>
<operation></operation>
<entry>
<observation>
<templateId/>
<code></code>
<value></value>
<entryRelationship>
<observation>
<templateId/>
<code></code>
<value></value>
</observation>
</entryRelationship>
<entryRelationship>
<observation>
<templateId/>
<code></code>
<value></value>
</observation>
</entryRelationship>
</observation>
</entry>
</content>
I thought I could get all three observation nodes using
foreach (XElement element in Content.Descendants("observation"))
ExamineObservation(element);
Although it looks like this only works when observation does not have children. I also tried .Ancestors and .DecentantNodes, but didn't get what I wanted.
I can easily write a recursive method that gets me what I need, but I would rather use an existing method if there is one, especially since I will be working with XML quite a bit on several projects. Am I missing something obvious?
Note that any node that says observation, I will need to get the code and value from, so in the example below I will need to process three observation nodes. The nesting and quantity of observation nodes are arbitrary.
Thank you for any assistance.
ADDENDUM
It occurs to me that I may not be giving enough information about the XML. I didn't think tags would make a difference, but I suppose I should include them just in case. Below is the first several lines of an actual message I am attempting to parse. I did replace some text with "..." for privacy.
<?xml version="1.0" encoding="iso-8859-1"?>
<content xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<operation>update</operation>
<entry xmlns="urn:hl7-org:v3">
<observation classCode="OBS" moodCode="EVN">
<templateId root="..." />
<code code="..." codeSystem="..." codeSystemName="..." displayName="...">
</code>
<value xsi:type="..." code="..." codeSystem="..." codeSystemName="..." displayName="...">
</value>
<entryRelationship typeCode="...">
<observation classCode="..." moodCode="...">
I just ran this code in VS2012, and it hit the Console.WriteLine()
3 times, outputting the observation node and contents correctly:
XElement content = XElement.Parse(yourXmlStringWithNamespaceHeader);
foreach (XElement obs in content.Descendants("observation"))
Console.WriteLine(obs.ToString());
Edit - taking into account the new namespace information, and using XDocument
instead of XElement
:
XNamespace nse = "urn:hl7-org:v3";
XDocument content = XDocument.Parse(yourXmlStringWithNamespaceHeader);
foreach (XElement ele in content.Descendants(nse + "observation"))
Console.WriteLine(ele.ToString());
这篇关于查找节点递归在一个XDocument的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!