简体   繁体   English

从XML元素中获取具有值的空值

[英]Getting null value from XML element with value

I have a XML structured like this: 我有这样的XML结构:

<airports>
  <airport code="code">
  Airport name
    <location>Airport location</location>
   </airport>
...
</airports>

And I am trying to parse its code and name: 我正在尝试解析其代码和名称:

List<string> list = new List<string>();
XmlDocument xDoc = new XmlDocument();
xDoc.Load("file.xml");

foreach (XmlNode node in xDoc.GetElementsByTagName("airport"))
{
    list.Add(node.Attributes["code"] + " " + node.Value);
}

But instead of the value I am not getting anything. 但是,除了价值,我什么也没得到。 When debugging, it says, the value of the node in null . 它表示在调试时,节点的null Yet, I can see the text in .InnerText . 但是,我可以在.InnerText看到文本。 Can you tell me, where is the problem and how can I get the value? 您能告诉我问题出在哪里,我如何获得价值?

Try replacing node.Value with node.FirstChild.Value . 尝试将node.Value替换为node.FirstChild.Value

That should return something like: 那应该返回类似:

"\r\n        Airport name\r\n        "

Well you could have probably just used innertext , but as Grant Winney alluded to the "value" of the airport node is a child node of type (text) of the airport node. 好吧,您可能只使用了innertext ,但是正如Grant Winney提到机场节点的“值”那样,它是机场节点类型(文本)的子节点。

It seems strange but it was a way of dealing with xml like this 看起来很奇怪,但这是处理xml的一种方式

<NodeA>Fred<NodeB>Bloggs</NodeB></NodeA>

ie NodeA has two children, one of type text and another of type elements. 也就是说,NodeA有两个孩子,一个是文本类型,另一个是类型元素。 Other node types also fit in nicely. 其他节点类型也很合适。

What Grant Winney said will solve your issue. 格兰特·温尼所说的将解决您的问题。 But is there any reason why you're not using LINQ 2 XML instead of XmlDocument? 但是,为什么没有使用LINQ 2 XML而不是XmlDocument的原因呢?

You can achieve what you're doing very quickly and easily with minimal code: 您可以用最少的代码非常快速,轻松地实现自己的工作:

XDocument.Load("file.xml")
    .Root
    .Elements("airport")
    .Select (s => s.Attribute("code").Value + " " + s.FirstNode)
    .ToList<string>();


Ideally though, if you have the opportunity, you should put 'airport name' into it's own element inside <airport> . 不过,理想情况下,如果有机会,应该在<airport>中将“机场名称”放入其自己的元素中。 Like this: 像这样:

 <airports> <airport code="code"> <name>Airport name</name> <location>Airport location</location> </airport> ... </airports> 

The issue is that an XmlElement, which is a specialization of XmlNode where NodeType is Element, has no "value" and thus always returns null. 问题是XmlElement是XmlNode的一种特殊化,其中NodeType是Element, 没有“值” ,因此总是返回null。

It has Attributes, and it has Child nodes. 它具有属性,并且具有子节点。 XmlElement.InnerText works because it recursively builds the result from the Children and grandchildren, etc (some of which are Text nodes). XmlElement.InnerText之所以起作用,是因为它以递归方式从子代和孙代(其中一些是Text节点)构建结果。

Remember that text sections in XML are really just Nodes themselves. 请记住,XML中的文本部分实际上只是节点本身。


Ideally, the XML would be fixed such that the name was an attribute (or even the sole [Text] node in an element). 理想情况下,将XML固定为名称是一个属性(甚至是元素中唯一的[Text]节点)。

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

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