读取xml节点的子标签值

huangapple go评论110阅读模式
英文:

Reading xmlnode child tag value

问题

  1. XmlDocument xmlDocNew = new XmlDocument();
  2. XmlElement CATLOG = xmlDocNew.CreateElement("mappings");
  3. XmlNode xmlNodeTab = xmltest.DocumentElement;
  4. XmlNodeList xmlNodeListCD = xmlNodeTab.SelectNodes("//mapping");
  5. foreach (XmlNode xmlNodeCD in xmlNodeListCD)
  6. {
  7. string innerText = xmlNodeCD["a"].InnerText;
  8. string xmlNodeapp = xmlNodeCD["b/c/d"].InnerText;
  9. }

在上面的代码中,我能够遍历所有的 "a" 标签元素,但是无法读取 "d" 标签的值。要读取 "d" 标签的值,你可以使用 "b/c/d" 的路径来访问,就像上面的代码示例中所示。这样,在 foreach 循环中,你可以同时获取 "a" 标签和其对应的 "d" 标签的值。

英文:

My xml file looks like below.

  1. <mappings>
  2. <mapping>
  3. <a>a1value</a>
  4. <b>
  5. <c>
  6. <d>d1value</d>
  7. <e>e1value</e>
  8. </c>
  9. </b>
  10. </mapping>
  11. <mapping>
  12. <a>a2value</a>
  13. <b>
  14. <c>
  15. <d>d2value</d>
  16. <e>e2value</e>
  17. </c>
  18. </b>
  19. </mapping>
  20. </mappings>

C# code to read the value of a,d tags.

  1. XmlDocument xmlDocNew = new XmlDocument();
  2. XmlElement CATLOG = xmlDocNew.CreateElement("mappings");
  3. XmlNode xmlNodeTab = xmltest.DocumentElement;
  4. XmlNodeList xmlNodeListCD = xmlNodeTab.SelectNodes("//mapping");
  5. foreach (XmlNode xmlNodeCD in xmlNodeListCD)
  6. {
  7. string innerText = xmlNodeCD["a"].InnerText;
  8. string xmlNodeapp = xmlNodeCD["//b/c/d"].InnerText;
  9. }

With the above code i am able to iterate through all the "a" tag elements.
But I am not able to read "d" tag value.
How do I read the value of "d" tag?

When I iterate through each element in the foreach loop, I want to get the value of "a" tag and its corresponding "d" tag value.

答案1

得分: 0

这段代码查看每个映射,并在其下查找标签 ad 的值。还要注意,如果您想查找“sub”标签并仅获取这些子项,您必须使用 .// 而不仅仅是 //。点符号提取位于当前节点下的节点。

  1. XmlDocument doc = new XmlDocument();
  2. doc.Load($@"{Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName}\json.txt");
  3. var nodes = doc.SelectNodes("//mapping");
  4. foreach (XmlNode node in nodes)
  5. {
  6. var aNodes = node.SelectNodes(".//a");
  7. foreach (XmlNode aNode in aNodes)
  8. Console.WriteLine(aNode.InnerText);
  9. // 如果您知道 .//b/c/d 下只有一个节点,请使用 SelectSingleNode。
  10. Console.WriteLine(node.SelectSingleNode(".//b/c/d").InnerText);
  11. }

输出

  1. a1value
  2. d1value
  3. a2value
  4. d2value

注意:

a 标签在某种程度上是 d 标签的兄弟,并且它们的单一父元素是 mapping 元素。如果您获取所有映射节点,然后逐个处理这些节点,您将获得最佳结果。

英文:

This code looks at each of the mapping and under it, looks up the value for tags a and d. Also note that if you want to look up 'sub' tags and only get these child items, you have to use .// instead of just //. Dot notation pulls node "under" the current node.

  1. XmlDocument doc = new XmlDocument();
  2. doc.Load($@"{Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName}\json.txt");
  3. var nodes = doc.SelectNodes("//mapping");
  4. foreach (XmlNode node in nodes)
  5. {
  6. var aNodes = node.SelectNodes(".//a");
  7. foreach (XmlNode aNode in aNodes)
  8. Console.WriteLine(aNode.InnerText);
  9. // If you know there will only be one node at .//b/c/d, use SelectSingleNode instead.
  10. Console.WriteLine(node.SelectSingleNode(".//b/c/d").InnerText);
  11. }

Output

  1. a1value
  2. d1value
  3. a2value
  4. d2value

Note:

a tag is sort of a sibling of d tag and their singular parent is the mapping element. You would get best results if you got all the mapping nodes and then worked through those.

答案2

得分: 0

自 .Net 3.5 开始,引入了 Linq 和 Linq To XML,使 XML 解析变得更加简便。使用 Linq To XML,您可以这样读取它:

  1. void Main()
  2. {
  3. var s = @"<mappings>
  4. <mapping>
  5. <a>a1value</a>
  6. <b>
  7. <c>
  8. <d>d1value</d>
  9. <e>e1value</e>
  10. </c>
  11. </b>
  12. </mapping>
  13. <mapping>
  14. <a>a2value</a>
  15. <b>
  16. <c>
  17. <d>d2value</d>
  18. <e>e2value</e>
  19. </c>
  20. </b>
  21. </mapping>
  22. </mappings>";
  23. var data = from x in XDocument.Parse(s).Descendants("mapping")
  24. select new {
  25. a = (string)x.Element("a"),
  26. d = (string)x.Element("b").Element("c").Element("d")
  27. };
  28. foreach (var e in data)
  29. {
  30. Console.WriteLine($"{e.a}, {e.d}");
  31. }
  32. }

如果您有一个与此结构匹配的类,如下所示:

  1. public class MyData
  2. {
  3. public string A { get; set; }
  4. public string D { get; set; }
  5. }

然后,您可以像这样使用这个类,而不是匿名类型:

  1. var data = from x in XDocument.Parse(s).Descendants("mapping")
  2. select new MyData {
  3. A = (string)x.Element("a"),
  4. D = (string)x.Element("b").Element("c").Element("d")
  5. };
  6. foreach (var e in data)
  7. {
  8. Console.WriteLine($"{e.A}, {e.D}");
  9. }

PS:由于您获得的是一个 IEnumerable,您可以直接将数据绑定到 DataGridViewListBox 等(...DataSource = data.ToList())。

英文:

Since .Net 3.5 there is Linq and Linq To XML which makes XML parsing easier. With Linq To XML you could read it as:

  1. void Main()
  2. {
  3. var s = @&quot;&lt;mappings&gt;
  4. &lt;mapping&gt;
  5. &lt;a&gt;a1value&lt;/a&gt;
  6. &lt;b&gt;
  7. &lt;c&gt;
  8. &lt;d&gt;d1value&lt;/d&gt;
  9. &lt;e&gt;e1value&lt;/e&gt;
  10. &lt;/c&gt;
  11. &lt;/b&gt;
  12. &lt;/mapping&gt;
  13. &lt;mapping&gt;
  14. &lt;a&gt;a2value&lt;/a&gt;
  15. &lt;b&gt;
  16. &lt;c&gt;
  17. &lt;d&gt;d2value&lt;/d&gt;
  18. &lt;e&gt;e2value&lt;/e&gt;
  19. &lt;/c&gt;
  20. &lt;/b&gt;
  21. &lt;/mapping&gt;
  22. &lt;/mappings&gt;&quot;;
  23. var data = from x in XDocument.Parse(s).Descendants(&quot;mapping&quot;)
  24. select new {
  25. a=(string)x.Element(&quot;a&quot;),
  26. d=(string)x.Element(&quot;b&quot;).Element(&quot;c&quot;).Element(&quot;d&quot;)
  27. };
  28. foreach (var e in data)
  29. {
  30. Console.WriteLine($&quot;{e.a}, {e.d}&quot;);
  31. }
  32. }

With a file, instead of .Parse, you would use .Load( filename ).

Output:

  1. a1value, d1value
  2. a2value, d2value

If you have a class matching this structure, like:

  1. public class MyData
  2. {
  3. public string A { get; set; }
  4. public string D { get; set; }
  5. }

Then instead of the anonymous type you could use this class like so:

  1. var data = from x in XDocument.Parse(s).Descendants(&quot;mapping&quot;)
  2. select new MyData {
  3. A=(string)x.Element(&quot;a&quot;),
  4. D=(string)x.Element(&quot;b&quot;).Element(&quot;c&quot;).Element(&quot;d&quot;)
  5. };
  6. foreach (var e in data)
  7. {
  8. Console.WriteLine($&quot;{e.A}, {e.D}&quot;);
  9. }

PS: Since you are getting an IEnumerable, you could directly bind data to a DataGridView, ListBox ... ( ...DataSource = data.ToList()).

huangapple
  • 本文由 发表于 2020年1月3日 15:20:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574630.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定