Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.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#_Asp.net_.net_Xml - Fatal编程技术网

C# 获取XML字符串的值

C# 获取XML字符串的值,c#,asp.net,.net,xml,C#,Asp.net,.net,Xml,嗨,我一直在做这个用c#写的asp.net网络表单,现在 在封装在字符串中的XML结果上获取股票价值。 我对XML知之甚少,下面是我的尝试 我有这个XML字符串 <Code>0</Code> <responseData> <LotDetails> <DEVICE>OH503/E-ICAM</DEVICE> <DEVICE12NC>340000064194</DEVIC

嗨,我一直在做这个用c#写的asp.net网络表单,现在 在封装在字符串中的XML结果上获取股票价值。 我对XML知之甚少,下面是我的尝试

我有这个XML字符串

<Code>0</Code>
<responseData>
    <LotDetails>
        <DEVICE>OH503/E-ICAM</DEVICE>
        <DEVICE12NC>340000064194</DEVICE12NC>
        <CONTAINERNAME>MBP001012700</CONTAINERNAME>
        <PACKAGE>SOT1207</PACKAGE>
    </LotDetails>
</responseData>
我需要得到
在c#中,我有这个代码

string result = tmpVal.GetQueryResult(System.Configuration.ConfigurationManager.AppSettings["queryname_CMSS"].ToString(), System.Configuration.ConfigurationManager.AppSettings["paramnames_CMSS"].ToString(), LotID).InnerXml.ToString();
     XmlDocument doc = new XmlDocument();
            doc.LoadXml(result);
            XmlNode idNode = doc.SelectSingleNode("//responseData/LotDetails/CONTAINERNAME");
我上面的代码返回了一个附加信息错误:有多个根元素。第1行,位置16。 看起来我没有在XML结果中获取根元素。 有人能帮我解决这个问题吗? 先谢谢你。
希望您理解我的意思。

您可以反序列化没有这样的父节点的xml

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<parent>" + result + "</parent>");
    //doc.LoadXml($"<parent>{result}</parent>");

    XmlNode idNode = doc.SelectSingleNode("//responseData/LotDetails/CONTAINERNAME");
XmlDocument doc=新的XmlDocument();
doc.LoadXml(“+result+”);
//LoadXml($“{result}”);
XmlNode idNode=doc.SelectSingleNode(“//responseData/LotDetails/CONTAINERNAME”);

或者您可以使用一个方法来执行。

我运行了您的代码,它会像预期的那样返回idNode。您确定要处理的就是这个xml吗?idNode.InnerText为您提供值。关于doc.LoadXml(结果);错误提示:如何填充结果?你能和你的xml数据共享设置结果的代码吗?好吧,这不是我得到的xml我更新了我的问题它仍然返回错误有多个根元素。第1行,位置16。在loadXml部分,我将再次更新我的问题使用+符号而不是$符号来连接字符串。请注意,如果提供的XML包含XML声明或DOCTYPE声明,则此操作将不起作用。正确的XML应该定义父节点,在这种情况下,不需要这样做。因为它是一个特定的用例,所以建议添加带有两个根元素的父元素。我完全同意你@MichaelKay的观点,如果你想这样处理XML,您最好尝试更正XML源代码。为什么我没有得到父项?假设它是这样的0OH503/E-ICAM 340000064194 MBP001012700 SOT1207根本原因问题在于第一行:string result=tmpVal.GetQueryResult(System.Configuration.ConfigurationManager.AppSettings[“queryname_CMSS”].ToString(),System.Configuration.ConfigurationManager.AppSettings[“paramnames_CMSS”].ToString(),LotID).InnerXml.ToString();您需要从配置文件XML中获取XML数据类型,而不是字符串。