从XML读取Code值时出现问题

时间:2020-05-23 09:51:21

标签: 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>
<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

1 个答案:

答案 0 :(得分:0)

尝试SelectSingleNodeNameSpace

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;
相关问题