这是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
答案 0 :(得分:0)
尝试SelectSingleNode
和NameSpace
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;