简体   繁体   English

从xml读取到字符串列表C#

[英]Read from xml to string list C#

I have these XML file: 我有这些XML文件:

<cteProc xmlns="http://www.portalfiscal.inf.br/cte" versao="1.04" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <CTe xmlns="http://www.portalfiscal.inf.br/cte">
        <infCte versao="1.04" Id="CTexxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx">
            <ide>
                <compl>
                    <emit>
                        <rem>
                            <CNPJ>11111111111</CNPJ>
                            <IE>2222222</IE>
                            <xNome>Teste</xNome>
                            <enderReme>
                                <xLgr>xxxxxxx xxxxxx</xLgr>
                                <nro>S/N</nro>
                                <xCpl>AREA C</xCpl>
                                <xBairro>PARQ. xxxxxx</xBairro>
                                <cMun>125455</cMun>
                                <xMun>xxxxxx</xMun>
                                <CEP>548848</CEP>
                                <UF>AA</UF>
                                <xPais>BRASIL</xPais>
                            </enderReme>
                            <infNFe>
                                **<chave>1</chave>**
                                **<chave>2</chave>**
                                **<chave>3</chave>**
                            </infNFe>
                        </rem>
                        <exped>
                            <CNPJ>2342342342342</CNPJ>
                            <IE>15342683242345480</IE>
                                ...........................

And I need to get values and put inside a string 我需要获取值并放入字符串中

I try to do this: 我尝试这样做:

var ListaChave = new List<string>();

var lista = (from c in xDoc.Descendants(ns + "/rem/chave") select c.Value).ToList();

foreach (string s in lista)
  {
    add the values.....
  }

But the s var is null. 但是s var为空。 I don´t know how to get these values. 我不知道如何获得这些价值。 Anybody can help me please!? 任何人都可以帮助我!

Use linq to xml 使用linq到xml

XDocument doc = XDocument.Load("XMLFile1.xml");

XNamespace ns = @"http://www.portalfiscal.inf.br/cte";

List<string> strList = doc.Descendants(ns+"rem").Descendants(ns+"chave").Select(e => e.Value).ToList();

and alternatively, you can have more control by doing things like 或者,您可以通过以下方式进行更多控制

You're missing an element in the path you passed to Descendants . 您在传递给Descendants的路径中缺少一个元素。 In your XML document the chave elements are children of infNFe. 在您的XML文档中,chave元素是infNFe的子元素。 Your LINQ query is looking for chave elements under "rem", and not finding any, hence the null result. 您的LINQ查询正在“ rem”下查找chave元素,但未找到任何元素,因此结果为空。

Change your query to this: 将查询更改为此:

var lista = (from c in xDoc.Descendants(ns + "/rem/infNFe/chave") 
             select c.Value).ToList();

And you should get what you're looking for, as long as the ns is set correctly. 只要ns设置正确,您就应该得到想要的东西。

You can try with this code by just adding the infNFe: 您可以通过添加infNFe来尝试以下代码:

var ListaChave = new List<string>();

var lista = (from c in xDoc.Descendants(ns + "/rem/infNFe/chave") 
             select c.Value).ToList();

foreach (string s in lista)
{
   add the values.....
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM