如何从xml名称空间节点检索属性值? [英] How to retrieve an attribute value from an xml namespace node?

查看:82
本文介绍了如何从xml名称空间节点检索属性值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个如下所示的xml文件:

Hi,
I have an xml file which looks like following:

<ch>
<it>
<a></a>
<c:det id="1234" hosted="true" >
</it>
<it>...</it>
</ch>


现在,我将文档获取为XElement.
我必须在


Now, I am getting the the document as XElement.
I have to fetch the value of hosted attribute inside

<c:det>

节点.
我怎么知道的?
我尝试使用LocalName属性,可能无法以某种方式找出确切的Xpath值.
您能以Xpath方式还是LINQ方式帮助我?
在此先感谢您.

node.
How acn I get that?
I tried using LocalName property,may be somehow was not able to figure out the exact Xpath value.
Can you please help me in either Xpath way or LINQ way?
Thanks in advance.

推荐答案

是否激发了您的灵感?

does that inspires you?

		static void Main(string[] args)
		{
			string xml = @"<ch>
<it>
<a></a>
<det id=""1234"" hosted=""true""/>
</it>
<it>...</it>
</ch>";
			var xxml = XElement.Parse(xml);

			var list = from e in xxml.Descendants("det")
					   from attr in e.Attributes()
					   select new { Node = e, Attr = attr };

			foreach (var item in list)
				Console.WriteLine("{0}, {1}={2}", item.Node.Name.LocalName, item.Attr.Name.LocalName, item.Attr.Value);
		}


这篇关于如何从xml名称空间节点检索属性值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆