【问题标题】:Extracting XML values in multidimensional array c#在多维数组c#中提取XML值
【发布时间】:2020-06-05 18:46:06
【问题描述】:

我有一个如下的 xml 文件,我需要提取值并将它们放入多维数组中。这个想法是,当每个根元素<Etiquette> 有多个标签<string> 时,我需要使用标签<string> 的每个不同值重复相同的其他值

   <?xml version="1.0" encoding="utf-8"?>
   <ArrayOfEtiquette xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Etiquette>
     <BgColor>#8075D1C5</BgColor>
     <BorderColor>#FF4E5B6F</BorderColor>
     <AssociatedAffaireId>
       <string>d4689f33-5600-47fe-883d-efcbf5e469c2</string>
       <string>1bae35dd-d501-4d87-bdd4-147fc0ba29d2</string>
     </AssociatedAffaireId>
     <Label>Ouverte</Label>
    </Etiquette>
    <Etiquette>
     <BgColor>#80949CA8</BgColor>
     <BorderColor>#FF155E70</BorderColor>
     <AssociatedAffaireId>
       <string>203cc4a8-8c24-4a2d-837c-29c7c1f73007</string>
     </AssociatedAffaireId>
     <Label>Fermée</Label>
    </Etiquette>
   </ArrayOfEtiquette>

想要的结果:

{"#8075D1C5","#FF4E5B6F","d4689f33-5600-47fe-883d-efcbf5e469c2","Ouverte"}
{"#8075D1C5","#FF4E5B6F","1bae35dd-d501-4d87-bdd4-147fc0ba29d2","Ouverte"}
{"#80949CA8","#FF155E70","203cc4a8-8c24-4a2d-837c-29c7c1f73007","Fermée"}

问候,

【问题讨论】:

  • 你有什么尝试自己解决的?请看XDocument

标签: c# .net xml multidimensional-array


【解决方案1】:

使用 Xml Linq:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication157
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            var results = doc.Descendants("Etiquette")
                .SelectMany(x => x.Descendants("string")
                    .Select(y => new { BgColor = (string)x.Element("BgColor"), BorderColor = (string)x.Element("BorderColor"), UID = (string)y }))
                .ToList();
        }
    }
}

我只想要一个数组而不是匿名类型使用:

new string[] { (string)x.Element("BgColor"), (string)x.Element("BorderColor"), (string)y }

【讨论】:

    【解决方案2】:

    你可以试试XDocument

    XDocument xdoc = XDocument.Load("XMLFile7.xml");
    var mdAarray = xdoc.Descendants("Etiquette")
                       .SelectMany(etiquette => 
                                   etiquette.Descendants("string")
                                            .Select(associatedaffaire => new string[] {
                                              etiquette.Element("BgColor").Value.ToString(),
                                              etiquette.Element("BorderColor").Value.ToString(),
                                              associatedaffaire.Value.ToString(),
                                              etiquette.Element("Label").Value.ToString() }))
                        .ToArray();
    
    Console.WriteLine(JsonConvert.SerializeObject(mdAarray));
    

    输出

    [
     ["#8075D1C5","#FF4E5B6F","d4689f33-5600-47fe-883d-efcbf5e469c2","Ouverte"],
     ["#8075D1C5","#FF4E5B6F","1bae35dd-d501-4d87-bdd4-147fc0ba29d2","Ouverte"],
     ["#80949CA8","#FF155E70","203cc4a8-8c24-4a2d-837c-29c7c1f73007","Fermée"]
    ]
    

    【讨论】:

      【解决方案3】:

      您只需要先在Etiquette 上进行迭代,然后在AssociatedAffaireId 上再次进行迭代

      每次你可以在数组或列表中插入(为了简单起见,我将使用列表)

          XDocument xdoc = XDocument.Load("pathToXml.xml");
          // iterate all Etiquette elements
          foreach (var etiquette in xdoc.Root.Elements("Etiquette"))
          {
              // store common values
              string bgColor = etiquette.Element("BgColor").Value;
              string borderColor = etiquette.Element("BorderColor").Value;
              string label = etiquette.Element("Label").Value;
              // iterate all AssociatedAffaireId.string elements and add to list
              var associatedAffaireIdEl = etiquette.Element("AssociatedAffaireId");
              foreach (var associatedAffaireId in associatedAffaireIdEl.Elements("string"))
              {
                  string aaid = associatedAffaireId.Value;
                  listOfArray.Add(new string[]{bgColor, borderColor, aaid, label});
              }
          }
      

      我希望这会有所帮助。

      抱歉,我发现了一些错误。看看我的小提琴here

      【讨论】:

        猜你喜欢
        • 2012-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-19
        • 2013-06-15
        • 1970-01-01
        相关资源
        最近更新 更多