【问题标题】:Problem while reading the value of Code from XML [closed]从 XML 读取代码值时出现问题 [关闭]
【发布时间】:2020-05-23 09:51:21
【问题描述】:

这里是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

【问题讨论】:

  • 您没有展示您是如何尝试读取这些值的,这使得帮助您变得更加困难。我建议尽管使用 LINQ to XML (XDocument),因为它使用 API 要简单得多。我建议从阅读docs.microsoft.com/en-us/dotnet/csharp/programming-guide/… 开始,然后尝试应用它,并编辑您的问题以显示您尝试过的内容以及您遇到的问题(如果您仍然卡住)。跨度>

标签: c# asp.net xml parsing soap


【解决方案1】:

试试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;

【讨论】:

  • 由于命名空间而无法正常工作
  • 通过添加命名空间更新帖子
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多