Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获取字符串形式的xml节点值_C#_.net_Xml - Fatal编程技术网

C# 如何获取字符串形式的xml节点值

C# 如何获取字符串形式的xml节点值,c#,.net,xml,C#,.net,Xml,我尝试使用以下代码获取特定节点的值,但在加载xml时引发了此异常: 例外情况: XmlDocument xml = new XmlDocument(); filePath = @"D:\Work_Time_Calculator\10-07-2013.xml"; xml.LoadXml(filePath); // Exception occurs here XmlNode node = xml.SelectSingleNode("/Data[@*]/Short_Fall"); string i

我尝试使用以下代码获取特定节点的值,但在加载xml时引发了此异常:

例外情况:

XmlDocument xml = new XmlDocument();
filePath = @"D:\Work_Time_Calculator\10-07-2013.xml";
xml.LoadXml(filePath);  // Exception occurs here 
XmlNode node = xml.SelectSingleNode("/Data[@*]/Short_Fall");
string id = node["Short_Fall"].InnerText;
XmlDocument xml = new XmlDocument();
filePath = @"D:\Work_Time_Calculator\10-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.")
根级别的数据无效。第1行,位置1

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#:

XmlDocument xml = new XmlDocument();
filePath = @"D:\Work_Time_Calculator\10-07-2013.xml";
xml.LoadXml(filePath);  // Exception occurs here 
XmlNode node = xml.SelectSingleNode("/Data[@*]/Short_Fall");
string id = node["Short_Fall"].InnerText;
XmlDocument xml = new XmlDocument();
filePath = @"D:\Work_Time_Calculator\10-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.")

您应该使用
.Load
而不是
.LoadXML

“LoadXml方法用于直接加载XML字符串。您想改用Load方法。”


ref:

代码中的问题是
xml.LoadXml(filePath)

LoadXml
方法将参数作为xml数据而不是xml文件路径

试试这个代码

   string xmlFile = File.ReadAllText(@"D:\Work_Time_Calculator\10-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;
                }
编辑

看到你问题的最后一次编辑我找到了答案

只需更换以下两条线路

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.")


它应该可以解决您的问题,或者您可以使用我之前提供的解决方案

这些帖子帮助我克服了我在创建CLR存储过程时遇到的几个问题,该过程使用了针对Inform3API的RESTfulAPI调用

XmlDocument d = new XmlDocument();
d.Load(@"D:\Work_Time_Calculator\10-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\10-07-2013.xml");
XmlNodeList n = d.GetElementsByTagName("Short_Fall");
if(n != null) {
    foreach(XmlNode curr in n) {
        Console.WriteLine(curr.InnerText);
    }
}
这些API的XML结果如下所示:

miResult xmlns="http://lawson.com/m3/miaccess">
    <Program>MMS200MI</Program>
    <Transaction>Get</Transaction>
    <Metadata>...</Metadata>
    <MIRecord>
        <RowIndex>0</RowIndex>
        <NameValue>
            <Name>STAT</Name>
            <Value>20</Value>
        </NameValue>
        <NameValue>
            <Name>ITNO</Name>
            <Value>ITEM123</Value>
        </NameValue>
        <NameValue>
            <Name>ITDS</Name>
            <Value>ITEM DESCRIPTION 123 </Value>
        </NameValue>
...

希望这能提供帮助。

为什么不使用LINQ to XML的可能副本?与旧的
XmlDocument
类相比,它更容易开发,也更友好。我使用了Load方法,现在不会抛出异常,但会在string id=node[“Short_Fall”]行抛出“Object reference not set to a instance of a Object.”;我认为这是因为您已经像在前一行xml中一样处于短缺状态;因此,如何获取特定的节点值请直接尝试node.InnerText