How to read attributes of a line in XML?

Asked

Viewed 1,304 times

-2

I have an XML file with the following structure:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<FIELDS>
<FIELD attrname="CO_UNIDADE" fieldtype="string" WIDTH="31"/>
<FIELD attrname="NO_FANTASIA" fieldtype="string" WIDTH="60"/>
<FIELD attrname="CO_MUNICIPIO_GESTOR" fieldtype="string" WIDTH="7"/>
<FIELD attrname="NU_CNPJ" fieldtype="string" WIDTH="14"/>
<FIELD attrname="CO_CNES" fieldtype="string" WIDTH="7"/>
<FIELD attrname="DT_ATUALIZACAO" fieldtype="datetime"/>
<FIELD attrname="TP_UNIDADE" fieldtype="string" WIDTH="2"/>
</FIELDS>
<PARAMS LCID="2057"/></METADATA>
<ROWDATA>
<ROW CO_UNIDADE="3304559061991" NO_FANTASIA="CENTO ODONTOLOGICO MEIER" CO_MUNICIPIO_GESTOR="330455" NU_CNPJ="01030615000123" CO_CNES="9061991" DT_ATUALIZACAO="20160902" TP_UNIDADE="22"/>
<ROW CO_UNIDADE="3304559062920" NO_FANTASIA="UROCOPA SERVICOS MEDICOS" CO_MUNICIPIO_GESTOR="330455" NU_CNPJ="24521838000199" CO_CNES="9062920" DT_ATUALIZACAO="20160905" TP_UNIDADE="36"/>
<ROW CO_UNIDADE="3304559065202" NO_FANTASIA="CENTRO ODONTOLOGICO LARANJEIRAS" CO_MUNICIPIO_GESTOR="330455" NU_CNPJ="86714722000123" CO_CNES="9065202" DT_ATUALIZACAO="20160929" TP_UNIDADE="22"/>
<ROW CO_UNIDADE="3534409062696" NO_FANTASIA="JUNG HI CHOI" CO_MUNICIPIO_GESTOR="353440" NU_CPF="08666567821" CO_CNES="9062696" DT_ATUALIZACAO="20160906" TP_UNIDADE="22"/>
...

How do I get the data from the line NO_FANTASIA and CO_CNES of the lines ROW?

  • How are you reading the XML?

1 answer

2


It’s actually quite simple. The class XMLDocument has an attribute Attributes, look:

XmlDocument doc = new XmlDocument();
doc.LoadXml("<teste atributo=\"batata\">uma simples tag xml</teste>");

XmlElement root = doc.DocumentElement;

string s = root.Attributes["atributo"].Value;
Console.WriteLine(s);

I left a functional example on Rextester

Browser other questions tagged

You are not signed in. Login or sign up in order to post.