Xml 未找到元素
Xml element not found
我正在尝试操作 XML 文件,但我遇到了问题。
目前,我正在尝试获取下面突出显示的标签值,但我没有获取它。
XML 文件
<!-- language:xml -->
<cteProc versao="2.00" xmlns="http://www.portalfiscal.inf.br/cte">
<CTe xmlns="http://www.portalfiscal.inf.br/cte">
<infCte versao="2.00" Id="CTe35170208310367000113570010001828261272183608">
<ide>
<cUF>35</cUF>
<cCT>27218360</cCT>
<CFOP>5353</CFOP>
<natOp>Prest. Serv. Transporte A Estab. Comercial</natOp>
<forPag>2</forPag>
<mod>57</mod>
<serie>1</serie>
<nCT>182826</nCT>
<dhEmi>2017-02-13T08:57:27</dhEmi>
<tpImp>1</tpImp>
<tpEmis>1</tpEmis>
</ide>
<compl>
<Entrega>
<semData>
<tpPer>0</tpPer>
</semData>
<semHora>
<tpHor>0</tpHor>
</semHora></Entrega>
</compl>
<vPrest>
<vTPrest>2440.00</vTPrest>
<vRec>2440.00</vRec>
<Comp>
<xNome>Frete</xNome>
<vComp>2147.20</vComp>
</Comp>
</vPrest>
<infCTeNorm>
<infCarga>
<vCarga>0.00</vCarga>
<proPred>jkfhmjhmh</proPred>
<infQ>
<cUnid>00</cUnid>
<tpMed>LITRAGEM</tpMed>
<qCarga>1000.0000</qCarga>
</infQ>
</infCarga>
</infCTeNorm>
</infCte>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
<Reference URI="#CTe35170208310367000113570010001828261272183608">
<Transforms>
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
<Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
<DigestValue>VlX1bKhOHjQb5QCv6TliOoMFmwA=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>xxxxxxxxxxxx</SignatureValue>
<KeyInfo>
<X509Data><X509Certificate>xxxxxxxxxxxxxx</X509Certificate></X509Data>
</KeyInfo>
</Signature>
</CTe>
<protCTe versao="2.00">
<infProt>
<tpAmb>2</tpAmb>
<verAplic>SP-CTe-22-12-2016</verAplic>
**<chCTe>xxxxxxxxxxxxxxxxxxxxxxxxxx</chCTe>**
<dhRecbto>2017-02-13T09:09:08</dhRecbto>
<nProt>135170007940102</nProt>
<digVal>VlX1bKhOHjQb5QCv6TliOoMFmwA=</digVal>
<cStat>100</cStat>
<xMotivo>Autorizado o uso do CT-e</xMotivo>
</infProt>
</protCTe>
</cteProc>
我正在尝试获取标签 "<**chCTe>xxxxxxxxxxxxxxxxxxxxxxxxxx",但总是 return null
.
代码
XDocument doc = XDocument.Load("file_1.xml");
var c = from a in doc.Descendants("chCTe") select a;
var xDoc = XDocument.Parse(str);
XNamespace nsSys = "http://www.portalfiscal.inf.br/cte";
string value = xDoc.Descendants(nsSys + "chCTe").FirstOrDefault()?.Value;
value.Dump();
我正在尝试操作 XML 文件,但我遇到了问题。 目前,我正在尝试获取下面突出显示的标签值,但我没有获取它。
XML 文件
<!-- language:xml -->
<cteProc versao="2.00" xmlns="http://www.portalfiscal.inf.br/cte">
<CTe xmlns="http://www.portalfiscal.inf.br/cte">
<infCte versao="2.00" Id="CTe35170208310367000113570010001828261272183608">
<ide>
<cUF>35</cUF>
<cCT>27218360</cCT>
<CFOP>5353</CFOP>
<natOp>Prest. Serv. Transporte A Estab. Comercial</natOp>
<forPag>2</forPag>
<mod>57</mod>
<serie>1</serie>
<nCT>182826</nCT>
<dhEmi>2017-02-13T08:57:27</dhEmi>
<tpImp>1</tpImp>
<tpEmis>1</tpEmis>
</ide>
<compl>
<Entrega>
<semData>
<tpPer>0</tpPer>
</semData>
<semHora>
<tpHor>0</tpHor>
</semHora></Entrega>
</compl>
<vPrest>
<vTPrest>2440.00</vTPrest>
<vRec>2440.00</vRec>
<Comp>
<xNome>Frete</xNome>
<vComp>2147.20</vComp>
</Comp>
</vPrest>
<infCTeNorm>
<infCarga>
<vCarga>0.00</vCarga>
<proPred>jkfhmjhmh</proPred>
<infQ>
<cUnid>00</cUnid>
<tpMed>LITRAGEM</tpMed>
<qCarga>1000.0000</qCarga>
</infQ>
</infCarga>
</infCTeNorm>
</infCte>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
<Reference URI="#CTe35170208310367000113570010001828261272183608">
<Transforms>
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
<Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
<DigestValue>VlX1bKhOHjQb5QCv6TliOoMFmwA=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>xxxxxxxxxxxx</SignatureValue>
<KeyInfo>
<X509Data><X509Certificate>xxxxxxxxxxxxxx</X509Certificate></X509Data>
</KeyInfo>
</Signature>
</CTe>
<protCTe versao="2.00">
<infProt>
<tpAmb>2</tpAmb>
<verAplic>SP-CTe-22-12-2016</verAplic>
**<chCTe>xxxxxxxxxxxxxxxxxxxxxxxxxx</chCTe>**
<dhRecbto>2017-02-13T09:09:08</dhRecbto>
<nProt>135170007940102</nProt>
<digVal>VlX1bKhOHjQb5QCv6TliOoMFmwA=</digVal>
<cStat>100</cStat>
<xMotivo>Autorizado o uso do CT-e</xMotivo>
</infProt>
</protCTe>
</cteProc>
我正在尝试获取标签 "<**chCTe>xxxxxxxxxxxxxxxxxxxxxxxxxx",但总是 return null
.
代码
XDocument doc = XDocument.Load("file_1.xml");
var c = from a in doc.Descendants("chCTe") select a;
var xDoc = XDocument.Parse(str);
XNamespace nsSys = "http://www.portalfiscal.inf.br/cte";
string value = xDoc.Descendants(nsSys + "chCTe").FirstOrDefault()?.Value;
value.Dump();