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#_Asp.net_Xml_Parsing_Soap - Fatal编程技术网

C# 从XML读取代码值时出现问题

C# 从XML读取代码值时出现问题,c#,asp.net,xml,parsing,soap,C#,Asp.net,Xml,Parsing,Soap,这是xml string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?> <UserMLogin xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"http://tempuri.org/\"> <Code>1</Code> &

这是xml

string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>
<UserMLogin xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"http://tempuri.org/\"> 
<Code>1</Code>
<Message>success</Message>
<Name>athil</Name>  
</UserMLogin>";
我试过的

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml.ToString());
XmlNodeList nodes = doc.DocumentElement.SelectNodes("/Code");
我得到了什么

<?xml version="1.0" encoding="utf-8"?><UserMLogin xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">  <Code>1</Code>  <Message>success</Message>  <Name>athil</Name></UserMLogin>
我所期待的

1

尝试使用
名称空间
选择SingleNode

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml.ToString());

XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("ns", "http://tempuri.org/");
string code = doc.SelectSingleNode("/ns:UserMLogin/ns:Code", nsmgr).InnerText;

你还没有展示你是如何试图阅读这些价值观的,这使得帮助你变得更加困难。我建议使用LINQtoXML(XDocument),因为它是一种更简单的API。我建议从阅读开始,然后尝试应用它,然后编辑你的问题,以显示你尝试了什么以及你在哪里卡住了(如果你仍然卡住了)。因为名称空间而无法工作通过添加名称空间更新了帖子