c# - 从 XML 中读取第一个根节点

标签 c# visual-studio-2010 visual-studio

我使用三种 XML 文件:

A 型:

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

B 型:

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

C 型:]

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

我尝试使用此代码读取第一个节点:

     XmlDocument xmlDoc = new XmlDocument();
     xmlDoc.Load(@"C:\crruopto\135120068964590_v01.04-procCTe.xml");
     XmlNodeList ml = xmlDoc.GetElementsByTagName("*");
     XmlElement root = xmlDoc.DocumentElement;
     exti = root.ToString();

但不返回任何我想读取第一个节点的内容,需要知道文件是 nfeProc 、canCTE 还是 cteProc 第二个问题是我如何从同一标签中的“value”中获取值???

谢谢

最佳答案

来自this帖子:

//Root node is the DocumentElement property of XmlDocument

XmlElement root = xmlDoc.DocumentElement

//If you only have the node, you can get the root node by

XmlElement root = xmlNode.OwnerDocument.DocumentElement

关于c# - 从 XML 中读取第一个根节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15690354/

相关文章:

c# - 如何在 C# 中自动插入可选的大括号?

C# Cosmos DB 补丁 - 将元素插入子集合

c# - ListItems 列表中的 DataBinding asp.net DropDownList 导致 System.ArgumentOutOfRangeException 错误

c# - 为什么 IntelliSense 'know' 没有关于我的数据表?

visual-studio - 如何在 Visual Studio 2010 中恢复文件(从光盘重新加载文件)

c# - 检查用户何时更改 Windows 玻璃刷(主题颜色)

c - 将一行存储为字符串并稍后在 C 代码中输出

visual-studio-2010 - mscorlib.dll 中发生了 'System.ServiceModel.CommunicationException' 类型的未处理异常

c++ - 找不到加载文件 OpenCVModules.cmake

c# - 在 Visual Studio 2015 UML 类图中生成代码不起作用