Extract XML tags C #

1

I was able to get a tag, but I did not find a way to get a repeat tag and store each repetition in a list.

XML Example

<casa>
  <porta>
     <janela>Janela 1</janela>
     <janela>Janela 2</janela>
     <janela>Janela 2</janela>
  </porta>
</casa>

Sample current code:

 var retorno = (from nota in xmlRetorno.Elements()
                       where nota.Name.LocalName.Equals("porta")
                       select nota).ToList();
    
asked by anonymous 11.10.2018 / 13:44

2 answers

2

You can create a class containing the properties of your object and then use the Deserialize class where it will get the XML information and convert directly to your object. This way your code stays cleaner and easier to maintain.

You can see examples from the community about this class in the links: link

link

A generic deserialize code that I've just built demonstrating how it works:

XML Example:

<?xml version="1.0" encoding="ISO-8859-1"?>
<CLIENTE>
 <ROW>
  <TABELA>CLIENTE</TABELA>
  <DATA_RECADASTRO/>
  <ID_CLIENTE>10262202</ID_CLIENTE>
  <ID_CLIENTE_RECADASTRO/>
  <NOME_CLIENTE>CLIENTE EXEMPLO 1</NOME_CLIENTE>
  <TIPO_CLIENTE>FIS</TIPO_CLIENTE>
 </ROW>
 <ROW>
  <TABELA>CLIENTE</TABELA>
  <DATA_RECADASTRO>20/03/2018</DATA_RECADASTRO>
  <ID_CLIENTE>10450769</ID_CLIENTE>
  <ID_CLIENTE_RECADASTRO/>
  <NOME_CLIENTE>CLIENTE EXEMPLO 2</NOME_CLIENTE>
  <TIPO_CLIENTE>FIS</TIPO_CLIENTE>
 </ROW>
 </CLIENTE>

public static T DeserializeObject<T>(string xml)
             where T : class
{
    if (string.IsNullOrEmpty(xml))
        throw new InvalidOperationException("Empty XML ERROR");

    using (var stringReader = new StringReader(xml))
    {
        var serializer = new XmlSerializer(typeof(T));
        return (T)serializer.Deserialize(stringReader);
    }
}

Note that the object class you are passing to the method must contain the DataNotations for each tag contained in the XML for the Deserialize to do the conversion. Example:

[Serializable]
    [XmlRoot(ElementName = "ROW")]
    public class ClientInfo
    {
        [XmlElement(ElementName = "TABELA")]
        public string NomeTabela { get; set; }

        [XmlElement(ElementName = "ID_CLIENTE", Type = typeof(int))]
        public int ClienteID { get; set; }

        [XmlElement(ElementName = "ID_CLIENTE_RECADASTRO")]
        public string ClientIDRecadastro { get; set; }

        [XmlElement(ElementName = "NOME_CLIENTE")]
        public string NomeCliente { get; set; }

        [XmlElement(ElementName = "TIPO_CLIENTE")]
        public string TipoCliente { get; set; }

        [XmlIgnore]
        public DateTime DataRecadastro { get; set; }

        [XmlElement("DATA_RECADASTRO")]
        public string SomeDateString
        {
            get { return this.DataRecadastro.ToString("dd-MM-yyyy"); }
            set {
                if (value != "")
                    this.DataRecadastro = DateTime.Parse(value);
            }
        }
    }
    
11.10.2018 / 16:07
2

To get the elements of XML you can use LINQ-to-XML, I made an example in a Console application using its XML and looked like this:

    using System.Linq;
    using System.Xml;
    using System.Xml.Linq;

    static void Main(string[] args)
    {            
        string xml = @"<casa>
                      <porta>
                         <janela>Janela 1</janela>
                         <janela>Janela 2</janela>
                         <janela>Janela 2</janela>
                      </porta>
                    </casa>";

        XDocument xDoc = XDocument.Parse(xml);
        var descendantsQuery = from desc in xDoc.Root.Descendants("janela")
                               select desc;

        foreach (var item in descendantsQuery)
        {
            Console.WriteLine(item.Value);
        }

        Console.ReadLine();
    }

In the line var descendantsQuery = from desc in xDoc.Root.Descendants("janela") we are informing that from the root element ( xDoc.Root that in its XML is "port") we want to get the descendants ( xDoc.Root.Descendants ) whose name is "window".

    
11.10.2018 / 18:58