带有命名空间的XML,怎么解析呢?

a121984376 2010-04-19 04:31:32

<?xml version="1.0" encoding="utf-8" ?>
<ArrayOfModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<Model>
<Name>Liu</Name>
<Numer>13211235578</Numer>
</Model>
<Model>
<Name>Yan</Name>
<Numer>13211235578</Numer>
</Model>
<Model>
<Name>Tony</Name>
<Numer>13211235578</Numer>
</Model>
<Model>
<Name>Jaz</Name>
<Numer>13211235578</Numer>
</Model>
</ArrayOfModel>


上面是通过一个webservice调用返回的结果,怎么解析呢? 我想解析实体类```
我用XElement.Descendants("Model")怎么查不出来呢?
...全文
340 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
wms102 2011-05-29
  • 打赏
  • 举报
回复
和规划规划规划个
a121984376 2010-04-20
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 liuyileneal 的回复:]
C# code

XmlDocument doc = new XmlDocument();
doc.Load(@"e:\model.xml");
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);

nsmgr.AddNam……
[/Quote]
解析成功,不过不是你的方法,给你分吧 ~呵呵~
liuyileneal 2010-04-19
  • 打赏
  • 举报
回复

XmlDocument doc = new XmlDocument();
doc.Load(@"e:\model.xml");
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);

nsmgr.AddNamespace("Name", doc.DocumentElement.NamespaceURI);
XmlNodeList nodeList;
XmlElement root = doc.DocumentElement;
nodeList = root.SelectSingleNode("//Name:ArrayOfModel", nsmgr).ChildNodes;
foreach (XmlNode node in nodeList)
{
foreach (XmlNode n in node.ChildNodes)
{
Console.WriteLine(n.InnerText);
}
}


帮你解决了,我在控制台输出了一下,你看你想怎么处理吧。。
liuyileneal 2010-04-19
  • 打赏
  • 举报
回复

using System;
using System.IO;
using System.Xml;

public class Sample
{
public static void Main()
{

XmlDocument doc = new XmlDocument();
doc.Load("booksort.xml");

//Create an XmlNamespaceManager for resolving namespaces.
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("bk", "urn:samples");

//Select and display the value of all the ISBN attributes.
XmlNodeList nodeList;
XmlElement root = doc.DocumentElement;
nodeList = root.SelectNodes("/bookstore/book/@bk:ISBN", nsmgr);
foreach (XmlNode isbn in nodeList){
Console.WriteLine(isbn.Value);
}

}

}

http://msdn.microsoft.com/en-us/library/4bektfx9(v=VS.100).aspx
liuyileneal 2010-04-19
  • 打赏
  • 举报
回复
上msdn,有函数的
zsuswy 2010-04-19
  • 打赏
  • 举报
回复
照解析不误
x276912757 2010-04-19
  • 打赏
  • 举报
回复

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧