3

Good Evening All, and happy weekend!.

I have been trying all day to understand how to parse my simple XML file so I can understand it enough to write a personal project I want to work on.

I have been reading articles on this site and others but cannot get past where I am :(

My XML Document is ...

<XML>
  <User>
    <ID>123456789</ID>
    <Device>My PC</Device>
  </User>
  <History>
    <CreationTime>27 June 2013</CreationTime>
    <UpdatedTime>29 June 2013</UpdatedTime>
    <LastUsage>30 June 2013</LastUsage>
    <UsageCount>103</UsageCount>
  </History>
  <Configuration>
    <Name>Test Item</Name>
    <Details>READ ME</Details>
    <Enabled>true</Enabled>   
  </Configuration>
</XML>

I am trying to get the value in the details element (READ ME). Below is my code

// Start Logging Progress
Console.WriteLine("Test Application - XML Parsing and Creating");
Console.ReadKey();

// Load XML Document
XmlDocument MyDoc = new XmlDocument();  MyDoc.Load(@"E:\MyXML.XML");

// Select Node
XmlNode MyNode = MyDoc.SelectSingleNode("XML/Configuration/Details");

// Output Node Value
Console.WriteLine(String.Concat("Details: ", MyNode.Value));

// Pause
Console.ReadKey();

My console application is running and outputing "Target: " but not giving me the detail within the element.

Can somebody see why this is happening, and perhaps give me advice if I am completely off the wheel? I have no previous knowledge in reading XML files; hence where I am now :)

Thanks! Tom

4

4 回答 4

5

使用您的 XPATH 表达式

// Select Node
XmlNode MyNode = MyDoc.SelectSingleNode("XML/Configuration/Details");

您正在选择一个元素,因此 MyNode 的类型将是XmlElementValuean 的XmlElement类型始终是null(请参阅MSDN上),因此您需要使用XmlElement.InnerTextXmlElement.InnerXml不是。

所以将您的代码更改为

// Output Node Value
Console.WriteLine(String.Concat("Details: ", MyNode.InnerText));

或者您可以使用 XPATHtext()函数选择元素的内容,在这种情况下,MyNode 将是XmlText您获取其值的地方Value

// Select Node
XmlNode MyNode = MyDoc.SelectSingleNode("XML/Configuration/Details/text()");

// Output Node Value
Console.WriteLine(String.Concat("Details: ", MyNode.Value));

作为旁注,如果您无论如何都在 C# 中学习 XML 操作,您应该查看LINQ to XML,这是在 C# 中使用 XML 的另一种/更新的方法。

于 2013-06-29T18:37:44.767 回答
0

您可以为此使用 Xpath 库(您必须包含“System.Xml.XPath”):

 XmlDocument document = new XmlDocument();
         document.Load("MyXml.xml");
         XPathNavigator navigator = document.CreateNavigator();

        foreach (XPathNavigator nav in navigator.Select("//Details"))
         {
             Console.WriteLine(nav.Value);

        }

上面的代码遍历每个称为 (Details) 的节点提取信息并打印它。

于 2014-04-14T09:10:45.240 回答
0

只是出于兴趣,一个鲜为人知的“简单”语法是这样的:

XmlDocument myDoc = new XmlDocument();
myDoc.Load(@"D:\MyXML.XML");

string details = myDoc["XML"]["Configuration"]["Details"].InnerText;

请注意,如果您的 XML 不符合您所期望的结构,这(和 XPath 方法)可能会流行,因此您最好也在那里进行一些验证。

于 2013-06-29T23:20:53.900 回答
0

如果要从 XML 文件中检索特定值

 XmlDocument _LocalInfo_Xml = new XmlDocument();
            _LocalInfo_Xml.Load(fileName);
            XmlElement _XmlElement;
            _XmlElement = _LocalInfo_Xml.GetElementsByTagName("UserId")[0] as XmlElement;
            string Value = _XmlElement.InnerText;

值包含文本值

于 2020-10-12T11:54:33.790 回答