0

I want to know which is the best way to iterate through XML file and get all elements(name & value) and attributes(name & value) keep in mind the root and elements names.

I would like to get a result like this per each element:

Id = 1
Nombre = Test 2
EAN = 8124 
Desc = 
Grupo =
Cod = 10009 
Stock = 3 

XML file example:

 <?xml version="1.0" encoding="utf-8"?>
  <Arts>
    <Art Id="1">
      <Nombre>Test 1</Nombre>
      <EAN>534532</EAN>
      <Desc />
      <Grupo />
      <Cod>10000</Cod>
      <Stock>29</Stock>
    </Art>
    <Art Id="2">
      <Nombre>Test 2</Nombre>
      <EAN>8124</EAN>
      <Desc />
      <Grupo />
      <Cod>10009</Cod>
      <Stock>3</Stock>
    </Art>
  </Arts>

I show you my code, I have two different options using XmlReader and XDocument:

public static string GetXmlSample(string xmlFileLocation, string rootXml, string elementXml) {
    XMLFileSampleResponse response = new XMLFileSampleResponse();

    XmlReaderSettings settings = new XmlReaderSettings();
    settings.IgnoreWhitespace = true;

    using(XmlReader xmlReader = XmlReader.Create(xmlFileLocation, settings)) {
        if (!xmlReader.ReadToFollowing(rootXml)) {
            throw new InvalidOperationException("Invalid parameter:  Root");
        }

        if (!xmlReader.ReadToDescendant(elementXml)) {
            throw new InvalidOperationException("Invalid parameter:  Element");
        }

        while (xmlReader.Read()) {
            string value = null;
            string header = null;

            switch (xmlReader.NodeType) {
            case XmlNodeType.Element:
                if (xmlReader.HasAttributes) {
                    for (int i = 0; i < xmlReader.AttributeCount; i++) {
                        xmlReader.MoveToAttribute(i);

                        header = xmlReader.Name;
                        value = xmlReader.Value;

                        response.headers.Add(header);
                        response.samples.Add(value);
                    }

                    xmlReader.MoveToElement();
                }

                header = xmlReader.LocalName;

                response.headers.Add(header);

                if (xmlReader.IsEmptyElement) {
                    response.samples.Add("");
                }

                break;
            case XmlNodeType.Text:
                value = xmlReader.Value;
                response.samples.Add(value);
                break;
            }
        }
    }

    return new JavaScriptSerializer().Serialize(response);
}

And:

public List <IDictionary<string,object>> GetXmlSampleDictionary(string xmlFileName, string rootXml, string elementXml) {
    List <IDictionary<string,object>> xmlProducts = new List <IDictionary<string, object>> ();

    var doc = XDocument.Load(xmlFileName);

    foreach(var element in doc.Root.Descendants(elementXml)) {
        dynamic expandoObject = new ExpandoObject();
        var dictionary = expandoObject as IDictionary <string, object> ;

        foreach(var child in element.Descendants()) {
            dictionary[child.Name.LocalName] = child.Value.Trim();
        }

        foreach(var child in element.Attributes()) {
            dictionary[child.Name.LocalName] = child.Value.Trim();
        }

        xmlProducts.Add(dictionary);
    }

    return xmlProducts;
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Cristian18
  • 220
  • 3
  • 12

0 Answers0