[英]How to Get The Values Printed from XML using LINQ or XPath
我使用XPath或LINQ获取从XML打印的值也很困难,这也是XPath和XML Parsing的新增内容,在这里看了几个示例,但没有任何帮助,因为它们都具有不同属性名称的XML,对于我的情况,XML是不同的:
<Entities TotalResult="3">
<Entity Type="test-set-folder">
<Fields>
<Field Name="id">
<Value>12457</Value>
</Field>
<Field Name="parent-id">
<Value>0</Value>
</Field>
<Field Name="last-modified">
<Value>2015-03-09 14:09:57</Value>
</Field>
<Field Name="description">
<Value/>
</Field>
<Field Name="view-order"/>
<Field Name="name">
<Value>.NET</Value>
</Field>
</Fields>
</Entity>
<Entity Type="test-set-folder">
<Fields>
<Field Name="id">
<Value>15487</Value>
</Field>
<Field Name="parent-id">
<Value>0</Value>
</Field>
<Field Name="last-modified">
<Value>2015-03-15 13:00:02</Value>
</Field>
<Field Name="description">
<Value/>
</Field>
<Field Name="view-order"/>
<Field Name="name">
<Value>Python</Value>
</Field>
</Fields>
</Entity>
</Entities>
它具有相同的属性Field
但Name
不同。
有人可以帮我打印属性Name
及其Value
。
我需要遍历每个Entity并打印其Name : Value
。
到目前为止,我尝试过使用Xpath和LINQ:
var doc = XDocument.Load("XMLFile1.xml");
foreach (var child in doc.Element("Field Name").Elements())
{
Console.WriteLine(child.Name);
}
但严重的是我对此并不太了解,所以我的方法完全错了。
我的预期输出是:
Id: 12457
parent-id: 0
last-modified:2015-03-09 14:09:57
description:
view-order:
name: .NET
并且对于下一个实体也是如此。
由于某些Fields缺少Value元素,您可以尝试这样做:
XElement element = XElement.Load("XmlFile.xml");
var query = from field in element.Descendants("Field")
select new
{
NameAttribute = field.Attribute("Name").Value,
Value = field.Descendants("Value").Any() ?
field.Element("Value").Value : "No Value"
};
var namesAndValues = query.ToList()
您有一个数据透视表,因为您的数据是从两个标记(名称和值)中提取的。 这是一种解析xml的方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<Entity> cEntities = new List<Entity>();
string input =
"<Entities TotalResult=\"3\">" +
"<Entity Type=\"test-set-folder\">" +
"<Fields>" +
"<Field Name=\"id\">" +
"<Value>12457</Value>" +
"</Field>" +
"<Field Name=\"parent-id\">" +
"<Value>0</Value>" +
"</Field>" +
"<Field Name=\"last-modified\">" +
"<Value>2015-03-09 14:09:57</Value>" +
"</Field>" +
"<Field Name=\"description\">" +
"<Value/>" +
"</Field>" +
"<Field Name=\"view-order\"/>" +
"<Field Name=\"name\">" +
"<Value>.NET</Value>" +
"</Field>" +
"</Fields>" +
"</Entity>" +
"<Entity Type=\"test-set-folder\">" +
"<Fields>" +
"<Field Name=\"id\">" +
"<Value>15487</Value>" +
"</Field>" +
"<Field Name=\"parent-id\">" +
"<Value>0</Value>" +
"</Field>" +
"<Field Name=\"last-modified\">" +
"<Value>2015-03-15 13:00:02</Value>" +
"</Field>" +
"<Field Name=\"description\">" +
"<Value/>" +
"</Field>" +
"<Field Name=\"view-order\"/>" +
"<Field Name=\"name\">" +
"<Value>Python</Value>" +
"</Field>" +
"</Fields>" +
"</Entity>" +
"</Entities>";
XElement entities = XElement.Parse(input);
foreach (XElement entity in entities.Descendants("Entity"))
{
Entity newEntity = new Entity();
cEntities.Add(newEntity);
foreach (XElement field in entity.Descendants("Field"))
{
string name = field.Attribute("Name").Value;
string value = field.Element("Value") == null ? "" : field.Element("Value").Value;
switch (name)
{
case "id" :
newEntity.id = int.Parse(value);
break;
case "parent-id":
newEntity.parent_id = int.Parse(value);
break;
case "last-modified":
newEntity.last_modified = DateTime.Parse(value);
break;
case "description":
newEntity.description = value;
break;
case "view-order":
newEntity.view_order = value;
break;
case "name":
newEntity.name = value;
break;
}
}
}
//write data
foreach(Entity entity in cEntities)
{
Console.WriteLine("id: {0}", entity.id);
Console.WriteLine("parent-id: {0}", entity.parent_id);
Console.WriteLine("last-modified: {0}",entity.last_modified);
Console.WriteLine("description: {0}",entity.description);
Console.WriteLine("view-order: {0}",entity.view_order);
Console.WriteLine("name: {0}",entity.name);
Console.WriteLine();
}
Console.ReadLine();
}
public class Entity
{
public int id { get; set; }
public int parent_id { get; set; }
public DateTime last_modified { get; set; }
public string description { get; set; }
public string view_order { get; set; }
public string name { get; set; }
}
}
}
您只需要遵循xml的结构
未经测试的代码:
var doc = XDocument.Load("XMLFile1.xml");
foreach (var fields in doc.Descendants("Fields")) {
foreach (var field in fields.Elements("Field")) {
Debug.WriteLine(string.Format("{0}: {1}", field.Attribute("Name").Value, field.Elements("Value").First().Value));
}
}
尝试这个
var names = from r in document.Descendants("Entities")
.Descendants("Entity")
.Descendants("Fields")
.Descendants("Field")
select new
{
Name = r.Element("name").Value,
};
foreach (var n in names)
{
Console.WriteLine(n.Name);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.