私はこのXMLファイルを持っています:
<?xml version="1.0" encoding="UTF-8"?>
<cteProc xmlns="http://www.portalfiscal.inf.br/cte" versao="1.04">
<CTe>
<emit>
<xFant>DOW</xFant>
<CNPJ>456789</CNPJ>
</emit>
<rem>
<CNPJ>777777</CNPJ>
<nCFOP>7101</nCFOP>
</rem>
</CTe>
<protCTe versao="1.04">
</cteProc>
このファイルを読み取るには、次の C# スニペットを使用します。
string
XmlTextReader reader = new XmlTextReader(@"C:\Conhecimento\158_v01.04-rocCTe.xml");
XmlNodeType type;
while (reader.Read())
{
type = reader.NodeType;
if (type == XmlNodeType.Element)
{
if (reader.Name == "xFant")
{ reader.Read(); XFant = reader.Value; textBox1.Text = XFant;}
if (reader.Name == "nCFOP")
{ reader.Read(); nCFOP = reader.Value; textBox2.Text = nCFOP;}
}
}
コードは非常にうまく機能しますが、子ノードの場合は機能しません<CNPJ>
。最初のみを読み取ります。このコードを記述して子孫を読み取るにはどうすればよいですか? のようなもので試しましたrem.CNPJ
。
他の質問から、私はこのコードを見ます:
文字列 CNPJ2、CNPJ3;
XNamespace ns = "http://www.portalfiscal.inf.br/cte";
var todas = from ide in XElement.Load(@"C:\136_v01.04.xml").Descendants(ns + "ide")
select new { CNPJ1 = (string)ide.Element(ns + "CNPJ") };
var emit = from ide1 in XElement.Load(@"C:\136_v01.04.xml").Descendants(ns + "emit")
select new { CNPJ2 = (string)ide1.Element(ns + "CNPJ") };
しかし、「todas」をint CNPJのような単一の変数に割り当てる方法