遍历所有XML元素和属性的最佳方法

时间:2019-07-24 15:12:22

标签: c# xml xmlreader xelement

我想知道哪种是迭代XML文件并获取所有元素(名称和值)和属性(名称和值)的最佳方法,请牢记根和元素名称。

我希望每个元素都得到这样的结果:

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

XML文件示例:

 <?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>

我向您展示我的代码,我使用XmlReaderXDocument有两个不同的选择:

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);
}

并且:

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;
}

0 个答案:

没有答案