简体   繁体   English

尝试读取XML文档时,节点返回null

[英]nodes are return null when trying to read XML document

i've programmically written an xml document to store some data and when i try to load it back into my application in a different area all of my Xmlnodes are returning null even though the node name i've given it is identical. 我已经以编程方式编写了一个xml文档来存储一些数据,并且当我尝试将其重新加载到其他区域的应用程序中时,即使我提供的节点名称相同,我的所有Xmlnodes都返回null。 This is preventing me from extracting the innertext of each node. 这使我无法提取每个节点的内部文本。

Question: What am i missing that is preventing me from reading this xml document 问题:我缺少什么,这使我无法阅读此xml文档

Code: 码:

var xmlDocument = new XmlDocument();
xmlDocument.Load(@"\\mi\dfs\shared\Everyone\The Guy Technology\cavanaugh\OutageInformationDocument.xml");

XmlNode title = xmlDocument.SelectSingleNode("TitleTextvariable");
XmlNode type = xmlDocument.SelectSingleNode("TypeTextvaraible");
XmlNode information = xmlDocument.SelectSingleNode("InformationText");
XmlNode conference = xmlDocument.SelectSingleNode("ConferenceText");
XmlNode steps = xmlDocument.SelectSingleNode("StepsText");
XmlNode eta = xmlDocument.SelectSingleNode("EtaText");
XmlNode phone = xmlDocument.SelectSingleNode("PhoneMessageText");
XmlNode banner = xmlDocument.SelectSingleNode("BannerText");

XML Example: XML示例:

<OutageInfo>
  <OutageInformation>
    <OutageInfoitems>
      <TitleTextvariable>title text</TitleTextvariable>
      <TypeTextvaraible>info</TypeTextvaraible>
      <InformationText>this is a test of the outage information</InformationText>
      <ConferenceText>information</ConferenceText>
      <StepsText>resolve it in this way</StepsText>
      <EtaText>30 minutes</EtaText>
      <PhoneMessageText>There is currently a phone message up</PhoneMessageText>
      <BannerText>There is not currently a banner posted</BannerText>
    </OutageInfoitems>
  </OutageInformation>
</OutageInfo>

You could use XElement : 您可以使用XElement

var xml = XElement.Load(pathToFile);
var infoItem = xml.Descendants("OutageInfoitems").First();
var title = (string)infoItem.Element("TitleTextvariable");

The casting element to string (in the last line) is preferable way, because if there's no such element, then title will be null rather throwing exception . 将元素强制转换为字符串(在最后一行中)是更可取的方式,因为如果没有这样的元素,则title将为null 而不是引发exception

The method you are using, XmlNode.SelectSingleNode(string xpath) , selects the first XmlNode that matches the XPath expression passed in as the argument value. 您使用的方法XmlNode.SelectSingleNode(string xpath) 选择与传入XPath表达式匹配的第一个XmlNode作为参数值。

Thus you need to use the XPath recursive descent operator // to descend your XML node hierarchy to pick out deeply nested nodes: 因此,您需要使用XPath递归下降操作符//来下降XML节点层次结构,以选择深度嵌套的节点:

XmlNode title = xmlDocument.SelectSingleNode("//TitleTextvariable");
XmlNode type = xmlDocument.SelectSingleNode("//TypeTextvaraible");
XmlNode information = xmlDocument.SelectSingleNode("//InformationText");
XmlNode conference = xmlDocument.SelectSingleNode("//ConferenceText");
XmlNode steps = xmlDocument.SelectSingleNode("//StepsText");
XmlNode eta = xmlDocument.SelectSingleNode("//EtaText");
XmlNode phone = xmlDocument.SelectSingleNode("//PhoneMessageText");
XmlNode banner = xmlDocument.SelectSingleNode("//BannerText");      

For more see Context for XPath Expressions : 有关更多信息,请参见XPath表达式的上下文

The evaluation of an XPath expression depends on the context against which the expression operates. XPath表达式的求值取决于表达式所针对的上下文。 The context consists of the node against which the expression is evaluated and its associated environment, which includes the following... 上下文由要针对其评估表达式的节点及其关联环境组成,其中包括以下内容:

Recursive descent 递归下降

An expression that uses the double forward slash (//) indicates a search that can include zero or more levels of hierarchy. 使用双正斜杠(//)的表达式表示搜索可以包括零个或多个层次结构。 When this operator appears at the beginning of the pattern, the context is relative to the root of the document. 当此运算符出现在模式的开头时,上下文是相对于文档根目录的。

Working .Net fiddle . 工作。 净提琴

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

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