Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从xml中读取特定节点_C#_Xml - Fatal编程技术网

C# 如何从xml中读取特定节点

C# 如何从xml中读取特定节点,c#,xml,C#,Xml,我在读取XML文件(C#)时遇到问题,问题是我可以读取cfdi:Comprobante中的所有元素,但当我想读取例如cfdi:emitor时,我无法获取信息,甚至使用: XmlDocument xml = new XmlDocument(); xml.Load(vnt.FileName); XmlElement root = xml.DocumentElement; //gets sub total from cfdi:Comprobante XmlAttribute total = r

我在读取XML文件(C#)时遇到问题,问题是我可以读取
cfdi:Comprobante
中的所有元素,但当我想读取例如
cfdi:emitor
时,我无法获取信息,甚至使用:

XmlDocument xml = new XmlDocument();

xml.Load(vnt.FileName);

XmlElement root = xml.DocumentElement;

//gets sub total from cfdi:Comprobante

XmlAttribute total = root.GetAttributeNode("subTotal");

// HERE IS THE BIG PROBLEM

XmlAttribute rfc = root.GetAttributeNode("cfdi:Comprobante/cfdi:Emisor/rfc");

string valor = total.InnerXml;

//string rfcE = rfc.InnerText; //HERE IS THE PROBLEM

dataGridView1.Rows[0].Cells[2].Value = valor;

// dataGridView1.Rows[0].Cells[1].Value = valor;


您需要在文档中添加名称空间表:

XmlNamespaceManager nsmgr = new XmlNamespaceManager(xml.NameTable);
nsmgr.AddNamespace("cfdi", "http://www.sat.gob.mx/cfd/3");
另外,我觉得您的XPath查询有点可疑。我想试试这个:

XmlElement emisor = (XmlElement)root.SelectSingleNode("cfdi:Emisor", nsmgr);
XmlAttribute rfc = emisor.GetAttributeNode("rfc");
修改的代码示例:

XmlDocument xml = new XmlDocument();

xml.Load(vnt.FileName);

// namespace manager added here
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xml.NameTable);
nsmgr.AddNamespace("cfdi", "http://www.sat.gob.mx/cfd/3");

XmlElement root = xml.DocumentElement;

//gets sub total from cfdi:Comprobante

XmlAttribute total = root.GetAttributeNode("subTotal");

// HERE IS THE BIG PROBLEM

// modified XPath query
XmlElement emisor = (XmlElement)root.SelectSingleNode("cfdi:Emisor", nsmgr);
XmlAttribute rfc = emisor.GetAttributeNode("rfc");

string valor = total.InnerXml;

//string rfcE = rfc.InnerText; //HERE IS THE PROBLEM

dataGridView1.Rows[0].Cells[2].Value = valor;

// dataGridView1.Rows[0].Cells[1].Value = valor;

通过这段代码,我得到了一个错误“xmlNode没有GetAttributeNode的定义”很好的捕获。。。您必须首先强制转换到
xmlement
。我修改了我的代码以反映这一点。在调试代码时仍然无法执行错误出现在这里“XmlElement emissor=(XmlElement)root.SelectSingleNode(“/cfdi:Comprobante/cfdi:emissor”);“我正在尝试不同的路径模式,但还没有尝试:更多修复。。。抱歉:(我打算在LINQPad中测试这个,但是现在知道,
SelectSingleNode
方法需要调用者提供名称表。我已经更新了我的代码并将进行测试。哇,伙计们,你帮了我很多,thak u:D
XmlDocument xml = new XmlDocument();

xml.Load(vnt.FileName);

// namespace manager added here
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xml.NameTable);
nsmgr.AddNamespace("cfdi", "http://www.sat.gob.mx/cfd/3");

XmlElement root = xml.DocumentElement;

//gets sub total from cfdi:Comprobante

XmlAttribute total = root.GetAttributeNode("subTotal");

// HERE IS THE BIG PROBLEM

// modified XPath query
XmlElement emisor = (XmlElement)root.SelectSingleNode("cfdi:Emisor", nsmgr);
XmlAttribute rfc = emisor.GetAttributeNode("rfc");

string valor = total.InnerXml;

//string rfcE = rfc.InnerText; //HERE IS THE PROBLEM

dataGridView1.Rows[0].Cells[2].Value = valor;

// dataGridView1.Rows[0].Cells[1].Value = valor;