[英]Extracting XML values in multidimensional array c#
我有一個如下的 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"}
問候,
使用 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 }
你只需要先在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});
}
}
我希望這會有所幫助。
抱歉,我發現了一些錯誤。 在這里查看我的小提琴。
您可以嘗試使用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"]
]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.