如何从没有属性的XML文件中获取节点并放入字符串列表 [英] How to get nodes from XML file without its attribute and put to a List of string
问题描述
我想显示没有其属性的子节点的标签名称.然后,应将这些标签名称(节点)放入字符串的List
中.这是我的XML文件的示例:
I would like to display the tag names of child nodes without its attributes. Then those tag names (nodes) should be put in a List
of string. Here's example of my XML file:
<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
<CAR>
<ID>21</ID>
<MANUFACTURER>Ford</MANUFACTURER>
<MODEL>Fiesta</MODEL>
</CAR>
<CAR>
<ID>22</ID>
<MANUFACTURER>Peugeot</MANUFACTURER>
<MODEL>508</MODEL>
</CAR>
</ROOT>
因此,我想在控制台输出中获得的效果如下所示:
So, the effect I want to get in a console output is shown below:
ID
MANUFACTURER
MODEL
然后,我要将ID,MANUFACTURER和MODEL标记名称存储在字符串的List
中.
Then I would like to store that ID, MANUFACTURER and MODEL tag names in a List
of strings.
这是我到目前为止尝试过的代码:
This is the code that I tried so far:
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.PreserveWhitespace = true;
try
{
xmlDocument.Load("XMLFile.xml");
}
catch (FileNotFoundException ex)
{
Console.WriteLine(ex);
}
Console.WriteLine(xmlDocument.OuterXml);
XmlNodeList nodeList = xmlDocument.SelectNodes("ROOT/CAR");
foreach(XmlNode node in nodeList)
{
Console.WriteLine(node.ChildNodes);
xmlNodes.Add(node.ChildNodes.ToString());
}
问题在于它没有显示我想要的方式.结果,我只得到两个似乎对应于两个<CAR>
节点的System.Xml.XmlChildNodes
,而不是它的三个子节点,例如ID,MANUFACTURER和MODEL.
The problem is that it's not displaying the way I want to. As a result I only get two System.Xml.XmlChildNodes
which seems to be corresponding to two <CAR>
nodes, instead of its three child nodes, such as ID, MANUFACTURER and MODEL.
System.Xml.XmlChildNodes
System.Xml.XmlChildNodes
将项目添加到列表中基本上会添加与上述相同的内容.
Adding items to a List basically adds the same thing as shown above.
我在做什么错了?
推荐答案
如果必须使用XmlDocument,则可以-
If you have to use XmlDocument, then you can -
List<string> elements = new List<string>();
XmlNodeList CarNodes = xml.SelectNodes("Root/Car");
foreach(XmlNode c in CarNodes)
{
foreach(XmlNode n in c.ChildNodes)
{
if (!elements.Contains(n.Name))
{
elements.Add(n.Name);
}
}
}
但是我发现 XDocument 更简单,可读性更好.
But I find XDocument to be much simpler and better readability.
XDocument xdoc = XDocument.Parse(yourXmlString);
List<string> elements = xdoc.Descendants("Car")
.DescendantNodes().OfType<XElement>()
.Select(x => x.Name).Distinct().ToList();
这就是您所需要的.同样易于阅读,获得汽车"节点的所有后代,并获得其中所有XElement的不同名称.
And thats all you'll need. Easy to read as well, get all the descendants of "Car" Node and get all distinct names of XElements within it.
另一种方法-
List<string> elements = xdoc.Descendants("Car").First()
.DescendantNodes().OfType<XElement>()
.Select(x => x.Name).ToList();
在这种情况下,我删除了"distinct",而只获得了第一个Car节点 ONLY .您可以看到差异-如果在任何情况下其他Car节点都具有额外的元素,则您将错过通过这种方式获取信息的机会.
In this case I have removed the "distinct" and rather got just the first Car node ONLY. You can see the difference - if by any case some other Car node has an extra element, you'll miss getting that information by doing it this way.
这篇关于如何从没有属性的XML文件中获取节点并放入字符串列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!