[英]Parsing XML using LINQ in c#
在這個 xml 文件 (http://www.studiovincent.net/list.xml) 中:
<list version="1.0">
<meta>
<type>resource-list</type>
</meta>
<resources start="0" count="4">
<resource classname="Quote">
<field name="name">Vincent</field>
<field name="username">Hill</field>
<field name="age">31</field>
<field name="hair">black</field>
</resource>
<resource classname="Quote">
<field name="name">John</field>
<field name="username">Tedelon</field>
<field name="age">27</field>
<field name="hair">brown</field>
</resource>
<resource classname="Quote">
<field name="name">Michael</field>
<field name="username">Lopez</field>
<field name="age">20</field>
<field name="hair">red</field>
</resource>
<resource classname="Quote">
<field name="name">Frank</field>
<field name="username">Lopez</field>
<field name="age">25</field>
<field name="hair">black</field>
</resource>
</resources>
</list>
使用此代碼:
using System.Xml;
using.System.Xml.Linq;
XmlReader reader = XmlReader.Create("http://www.studiovincent.net/list.xml");
XElement el = XElement.Load(reader);
reader.Close();
var items = el.Elements("resources").Elements("resource").Descendants().DescendantNodes();
var items = from item in el.Elements("resources").Elements("resource").Descendants()
where item.Attribute("name").Value == "name" select item.FirstNode;
foreach (XNode node in items)
{
Console.WriteLine(node.ToString());
}
我有這個輸出:
Vincent
John
Michael
Frank
代碼工作得很好,但我需要獲取值31 ,它對應於字段名稱 =“年齡”,其中字段名稱 =“名稱”是文森特。 我怎樣才能得到這個結果?
我建議您像自然地閱讀 XML 那樣做。
在您的代碼中,嘗試查找name
屬性設置為"name"
所有字段。
此過程不能用於將姓名與年齡相關聯。 讀取 XML 並檢查所有resource
元素會更自然。 然后向該元素添加field
元素中描述的一些信息。
// Using LINQ to SQL
XDocument document = XDocument.Load("http://www.studiovincent.net/list.xml"); // Loads the XML document.
XElement resourcesElement = document.Root.Element("resources"); // Gets the "resources" element that is in the root "list" of the document.
XElement resourceElementVincent = (from resourceElement in resourcesElement.Elements("resource")// Gets all the "resource" elements in the "resources" element
let fieldNameElement = resourceElement.Elements("field").Single(fieldElement => fieldElement.Attribute("name").Value == "name") // Gets the field that contains the name (there one and only one "name" field in the "resource" element -> use of Single())
where fieldNameElement.Value == "Vincent" // To get only resources called "Vincent"
select resourceElement).Single(); // We suppose there is one and only 1 resource called "Vincent" -> Use of Single()
XElement fieldAgeElement = resourceElementVincent.Elements("field").Single(fieldElement => fieldElement.Attribute("name").Value == "age"); // Gets the corresponding "age" field
int age = int.Parse(fieldAgeElement.Value, CultureInfo.InvariantCulture); // Gets the age by Parse it as an integer
Console.WriteLine(age);
它做你想要的嗎?
考慮以下 XML 作為 SQL 表的列之一。
<Root>
<Name>Dinesh</Name>
<Id>2</Id>
</Root>
查詢的目標是從 XML 中獲取 Name。 在本例中,我們將獲取“Dinesh”作為值。
var Query = (from t in dbContext.Employee.AsEnumerable()
where t.active == true
select new Employee
{
Id = t.AtpEventId,
Name = XDocument.Parse(t.Content).Descendants("Root").Descendants("Name").ToList().
Select(node => node.Value.ToString()).FirstOrDefault()
});
請注意以下事項:
t.active == true
只是在需要時設置一些條件的示例。
請注意,在上面的 LINQ 查詢中,始終使用 AsEnumerable,就像我在第一行中所做的那樣。
Descendants("Root").Descendants("Name")
- 這里的 "Root" 應該是與 XML 匹配的元素,在 Root 下我們有 Name 元素。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.