阅读从根节点XML值 [英] Reading a value from root node XML

查看:155
本文介绍了阅读从根节点XML值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的XML:
类型?答:

I have this XML : Type A:

 <?xml version="1.0" encoding="UTF-8"?>
 <nfeProc versao="2.00" xmlns="http://www.portalfiscal.inf.br/nfe">
 </nfeProc>



B型:

Type B:

<?xml version="1.0" encoding="UTF-8"?>
<cancCTe xmlns="http://www.portalfiscal.inf.br/cte" versao="1.04"> 
</cancCTe>



键入c:

Type C:

<?xml version="1.0" encoding="UTF-8"?>
<cteProc xmlns="http://www.portalfiscal.inf.br/cte" versao="1.04">
</cteProc> 



我已阅读根节点与此:

I have read the root node with this :

 XmlDocument xmlDoc = new XmlDocument();
 xmlDoc.Load(nomear);
 XmlNodeList ml = xmlDoc.GetElementsByTagName("*");
 XmlNode primer = xmlDoc.DocumentElement;
 exti = primer.Name;  



有了这个代码,我读 nfeProc cancTE cteProc

我怎么能读的versao

推荐答案

当你正在使用的 C#3.5或更高版本,您可以利用LINQ到XML(你的标签说,你正在使用C#4.0,因此它当然也适用)

As you are using C# 3.5 or later, you can take advantage of LINQ to XML (your tag says you are using C# 4.0, so it certainly applies)

//your xml contents. I've just escaped " symbols, so I can use it as literal
string str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n "+
                 "<nfeProc versao=\"2.00\" xmlns=\"http://www" +
                 ".portalfiscal.inf.br/nfe\">\r\n </nfeProc>";

var xml = XDocument.Parse(str);

Console.WriteLine(xml.Root.Attribute("versao").Value);

打印:

2.00

这篇关于阅读从根节点XML值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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