C# 如何获取字符串中的xml节点值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17590182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the xml node value in string
提问by Vignesh
I tried the below code to get the value of a particular node, but while loading the xml this exception is thrown:
我尝试了下面的代码来获取特定节点的值,但是在加载 xml 时抛出了这个异常:
Exception:
例外:
Data at the root level is invalid. Line 1, position 1.
根级别的数据无效。第 1 行,位置 1。
XML
XML
<?xml version="1.0"?>
<Data xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Date>11-07-2013</Date>
<Start_Time>PM 01:37:11</Start_Time>
<End_Time>PM 01:37:14</End_Time>
<Total_Time>00:00:03</Total_Time>
<Interval_Time/>
<Worked_Time>00:00:03</Worked_Time>
<Short_Fall>08:29:57</Short_Fall>
<Gain_Time>00:00:00</Gain_Time>
</Data>
C#:
C#:
XmlDocument xml = new XmlDocument();
filePath = @"D:\Work_Time_Calculator-07-2013.xml";
xml.LoadXml(filePath); // Exception occurs here
XmlNode node = xml.SelectSingleNode("/Data[@*]/Short_Fall");
string id = node["Short_Fall"].InnerText;
Modified Code
修改代码
C#:
C#:
XmlDocument xml = new XmlDocument();
filePath = @"D:\Work_Time_Calculator-07-2013.xml";
xml.Load(filePath);
XmlNode node = xml.SelectSingleNode("/Data[@*]/Short_Fall");
string id = node["Short_Fall"].InnerText; // Exception occurs here ("Object reference not set to an instance of an object.")
采纳答案by Rezoan
The problem in your code is xml.LoadXml(filePath);
您的代码中的问题是 xml.LoadXml(filePath);
LoadXmlmethod take parameter as xml datanot the xml file path
LoadXml方法将参数作为xml数据而不是xml文件路径
Try this code
试试这个代码
string xmlFile = File.ReadAllText(@"D:\Work_Time_Calculator-07-2013.xml");
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(xmlFile);
XmlNodeList nodeList = xmldoc.GetElementsByTagName("Short_Fall");
string Short_Fall=string.Empty;
foreach (XmlNode node in nodeList)
{
Short_Fall = node.InnerText;
}
Edit
编辑
Seeing the last edit of your question i found the solution,
看到你问题的最后编辑,我找到了解决方案,
Just replace the below 2 lines
只需替换以下 2 行
XmlNode node = xml.SelectSingleNode("/Data[@*]/Short_Fall");
string id = node["Short_Fall"].InnerText; // Exception occurs here ("Object reference not set to an instance of an object.")
with
和
string id = xml.SelectSingleNode("Data/Short_Fall").InnerText;
It should solve your problem or you can use the solution i provided earlier.
它应该可以解决您的问题,或者您可以使用我之前提供的解决方案。
回答by Siraj Mansour
回答by ravula sandeep
XmlDocument d = new XmlDocument();
d.Load(@"D:\Work_Time_Calculator-07-2013.xml");
XmlNodeList n = d.GetElementsByTagName("Short_Fall");
if(n != null) {
Console.WriteLine(n[0].InnerText); //Will output '08:29:57'
}
or you could wrap in foreach loop to print each value
XmlDocument d = new XmlDocument();
d.Load(@"D:\Work_Time_Calculator-07-2013.xml");
XmlNodeList n = d.GetElementsByTagName("Short_Fall");
if(n != null) {
foreach(XmlNode curr in n) {
Console.WriteLine(curr.InnerText);
}
}

