简体   繁体   English

使用C#通过XML文档的名称访问子节点

[英]Accessing a child node by name of an XML document using C#

I have a situation which an XML document contained something similar to the following, but then the order of the "Block" elements was changed, and in an application, I was accessing the value of the in by using GetElementsByTagName("Amount")[0].InnerText, as it was the first occurrence of "Amount". 我遇到这样的情况,一个XML文档包含类似于以下内容的内容,但是随后更改了“块”元素的顺序,在一个应用程序中,我正在使用GetElementsByTagName(“ Amount”)[]访问in的值。 0] .InnerText,因为它是“金额”的首次出现。 Then the order of the "Blocks" changed, and the "Amount" returned by the GetElementsByTagName was still the first occurrence, but I want the "Amount" in "Block1" to always be returned. 然后,“块”的顺序发生了变化,GetElementsByTagName返回的“数量”仍然是第一个出现,但是我希望始终返回“块1”中的“数量”。 How do I accomplish this in C#? 如何在C#中完成此操作?

Before: 之前:

<Response xmlns="testsite.com/schema/TestService/2015-01-01">
  <Block1>
     <Amount>$5.00</Amount>
  </Block1>
  <Block2>
     <Amount>$0.00</Amount>
  </Block2>
  <Block3>
     <Amount>$0.00</Amount>
  </Block3>
</Response>

After: 后:

<Response xmlns="testsite.com/schema/TestService/2015-01-01">
  <Block2>
     <Amount>$0.00</Amount>
  </Block2>
  <Block3>
     <Amount>$0.00</Amount>
  </Block3>
  <Block1>
     <Amount>$5.00</Amount>
  </Block1>
</Response>

C#: C#:

XmlDocument doc = new XmlDocument();
doc.LoadXml(response);

string amount = doc.GetElementsByTagName("Amount")[0].InnerText;

however, in my root element I have something like <Response xmlns="testsite.com/schema/TestService/2015-01-01"> 但是,在我的根元素中,我有类似<Response xmlns="testsite.com/schema/TestService/2015-01-01">

It is the default xmlnamespece of your xml. 它是xml的默认xmlnamespece。 With the new info you provided 使用您提供的新信息

XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
mgr.AddNamespace("ns", "testsite.com/schema/TestService/2015-01-01");

var value = doc.SelectSingleNode("/ns:Response/ns:Block1/ns:Amount", mgr).InnerText;

You can use method SelectSingleNode and XPath expression to access the data that you need. 您可以使用SelectSingleNode方法和XPath表达式来访问所需的数据。 You also need to use namespace manager and import correct namespace: 您还需要使用名称空间管理器并导入正确的名称空间:

var namespaceManager= new XmlNamespaceManager(doc.NameTable);
namespaceManager.AddNamespace("a", "testsite.com/schema/TestService/2015-01-01");

var xmlNode = doc.SelectSingleNode("/a:Response/a:Block1/a:Amount", namespaceManager);
var value = node.InnerText;    // prints $5.00

Such code would find you correct value regardless of where Block1 is - first, second or third element. 不管Block1在何处-第一个,第二个或第三个元素,此类代码都会为您找到正确的值。 The method SelectSingleNode returns first node that matches an expression, so if you have more than one Block1 element in your XML then the results might be not what you expect 方法SelectSingleNode返回与表达式匹配的第一个节点,因此,如果XML中有多个Block1元素,则结果可能不是您期望的

Start with System.Xml.Linq; 从System.Xml.Linq开始; You'll want to load your xml file and then parse your document. 您将要加载xml文件,然后解析您的文档。 For example, 例如,

var doc = XDocument.Load("file.xml");
IEnumerable<XElement> elements = doc.Descendants(tagNameHere);

And if you want to create a list of those descendants, you can access those elements by doing something like this: 如果要创建这些后代的列表,则可以通过执行以下操作来访问这些元素:

List<string> myElements = new List<string>();
XElement element = elements.ElementAt(0);
myElements.Add(element.Value);

This is just to get you started. 这只是为了让您入门。 I suggest you read here: 我建议你在这里阅读:

https://msdn.microsoft.com/en-us/library/system.xml.linq(v=vs.110).aspx https://msdn.microsoft.com/zh-CN/library/system.xml.linq(v=vs.110).aspx

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

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