我需要決議一個 XML 并將其存盤到一個類中,但它給我帶來的頭痛比我樂于承認的要多得多。
假設 XML 如下所示:
<PARENT>
<ITEM>
<DATA1>
<A>12345</A>
<B>12345</B>
<C>12345</C>
</DATA1>
<DATA2>
<D>12345</D>
<E>12345</E>
<F>12345</F>
</DATA2>
<DATA3>
<ITEM>
<G/>
<H>111</H>
<I>223</I>
</ITEM>
<ITEM>
<G/>
<H>2342</H>
<I>25323</I>
</ITEM>
<ITEM>
<G>185</G>
<H>63611</H>
<I/>
</ITEM>
</DATA3>
</ITEM>
<ITEM>
<DATA1>
<A>23456</A>
<B>23456</B>
<C>23456</C>
</DATA1>
</ITEM>
</PARENT>
我面臨的第一個問題是名稱“ ITEM ”不僅作為“ PARENT ”的子節點出現,而且在“ DATA3 ”下再次被重用,所以如果我像這樣搜索
XDocument doc = XDocument.Parse(tmp);
IEnumerable<XElement> tmp_list = doc.Descendants(XName.Get("ITEM"));
它會給我五個結果,而不僅僅是兩個。
我也已經嘗試過像這樣搜索路徑 //PARENT/ITEM 的方法:
string file = @"C:\temp\test.xml";
var document = XDocument.Load(file);
var namespaceManager = new XmlNamespaceManager(new NameTable());
namespaceManager.AddNamespace("empty", "random:namespace:name");
IEnumerable<XElement> ele_list = document.XPathSelectElements("//PARENT//ITEM", namespaceManager);
但它也不起作用,因為它還獲取所有孫節點......
問題 1)如何正確地將搜索限制在頂級節點?
Question 2) How would I then proceed and store the whole thing into a class in the most efficient way? I've tried copypasting the full XML response to websites like http://xmltocsharp.azurewebsites.net/ but all of them seem to be struggling with the duplicate name "ITEM" and basically create one class that has merged ALL of the different "ITEM" child nodes within. Is it possible to fill a List of items in a simple and efficient way? I was hoping to have essentially something along the lines of:
List<DataItem> grd_list = doc.Descendants(XName.Get("ITEM"))
.Select(f => new DataItem()
{
DATA1 = f.Element(XName.Get("DATA1")).Document.ToString(),
DATA2 = f.Element(XName.Get("DATA2")).Document.ToString(),
//etc.....
}).ToList();
but I'm aware that the above LINQ would have to be more complex than this since DATA1 etc also needs to have child nodes.
uj5u.com熱心網友回復:
您可以使用序列化程式 XML(它只是您的 xml 之后的一個示例)
您可以通過在 ITEM 類的定義中定義所有情況來避免專案問題。所以專案不存在(G、H、I 或 DATA1、DATA2.. 將為空)。
using System.Xml.Serialization;
XmlSerializer serializer = new XmlSerializer(typeof(PARENT));
using (StringReader reader = new StringReader(xml))
{
var test = (PARENT)serializer.Deserialize(reader);
}
:
:
[XmlRoot(ElementName = "DATA1")]
public class DATA1
{
[XmlElement(ElementName = "A")]
public string A { get; set; }
[XmlElement(ElementName = "B")]
public string B { get; set; }
[XmlElement(ElementName = "C")]
public string C { get; set; }
}
[XmlRoot(ElementName = "DATA2")]
public class DATA2
{
[XmlElement(ElementName = "D")]
public string D { get; set; }
[XmlElement(ElementName = "E")]
public string E { get; set; }
[XmlElement(ElementName = "F")]
public string F { get; set; }
}
[XmlRoot(ElementName = "ITEM")]
public class ITEM
{
[XmlElement(ElementName = "G")]
public string G { get; set; }
[XmlElement(ElementName = "H")]
public string H { get; set; }
[XmlElement(ElementName = "I")]
public string I { get; set; }
[XmlElement(ElementName = "DATA1")]
public DATA1 DATA1 { get; set; }
[XmlElement(ElementName = "DATA2")]
public DATA2 DATA2 { get; set; }
[XmlElement(ElementName = "DATA3")]
public DATA3 DATA3 { get; set; }
}
[XmlRoot(ElementName = "DATA3")]
public class DATA3
{
[XmlElement(ElementName = "ITEM")]
public List<ITEM> ITEM { get; set; }
}
[XmlRoot(ElementName = "PARENT")]
public class PARENT
{
[XmlElement(ElementName = "ITEM")]
public List<ITEM> ITEM { get; set; }
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/442383.html
標籤:c# xml xml-parsing xmlnode
