一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

C# Linq使用XDocument读取Xml文件并形成结构树数据(json)

时间:2022-06-25 08:13:36 编辑:袖梨 来源:一聚教程网

数据结构是这样的:

代码如下 复制代码




















































.....


如何快速的读取xml数据,并按照xml数据结构,形成C#实体数据集合那?

我试了好几个方法都不是很灵活,其中有一个XML操作类,可以分享给大家:

代码如下 复制代码

///

/// XmlHelper 的摘要说明

///

public class XmlHelper

{

public XmlHelper()

{

}

///

/// 读取数据

///

/// 路径

/// 节点

/// 属性名,非空时返回该属性值,否则返回串联值

/// string

/**************************************************

* 使用示列:

* XmlHelper.Read(path, "/Node", "")

* XmlHelper.Read(path, "/Node/Element[@Attribute='Name']", "Attribute")

************************************************/

public static string Read(string path, string node, string attribute)

{

string value = "";

try

{

XmlDocument doc = new XmlDocument();

doc.Load(path);

XmlNode xn = doc.SelectSingleNode(node);

value = (attribute.Equals("") ? xn.InnerText : xn.Attributes[attribute].Value);

}

catch { }

return value;

}

///

/// 插入数据

///

/// 路径

/// 节点

/// 元素名,非空时插入新元素,否则在该元素中插入属性

/// 属性名,非空时插入该元素属性值,否则插入元素值

///

///

/**************************************************

* 使用示列:

* XmlHelper.Insert(path, "/Node", "Element", "", "Value")

* XmlHelper.Insert(path, "/Node", "Element", "Attribute", "Value")

* XmlHelper.Insert(path, "/Node", "", "Attribute", "Value")

************************************************/

public static void Insert(string path, string node, string element, string attribute, string value)

{

try

{

XmlDocument doc = new XmlDocument();

doc.Load(path);

XmlNode xn = doc.SelectSingleNode(node);

if (element.Equals(""))

{

if (!attribute.Equals(""))

{

XmlElement xe = (XmlElement)xn;

xe.SetAttribute(attribute, value);

}

}

else

{

XmlElement xe = doc.CreateElement(element);

if (attribute.Equals(""))

xe.InnerText = value;

else

xe.SetAttribute(attribute, value);

xn.AppendChild(xe);

}

doc.Save(path);

}

catch { }

}

///

/// 修改数据

///

/// 路径

/// 节点

/// 属性名,非空时修改该节点属性值,否则修改节点值

///

///

/**************************************************

* 使用示列:

* XmlHelper.Insert(path, "/Node", "", "Value")

* XmlHelper.Insert(path, "/Node", "Attribute", "Value")

************************************************/

public static void Update(string path, string node, string attribute, string value)

{

try

{

XmlDocument doc = new XmlDocument();

doc.Load(path);

XmlNode xn = doc.SelectSingleNode(node);

XmlElement xe = (XmlElement)xn;

if (attribute.Equals(""))

xe.InnerText = value;

else

xe.SetAttribute(attribute, value);

doc.Save(path);

}

catch { }

}

///

/// 删除数据

///

/// 路径

/// 节点

/// 属性名,非空时删除该节点属性值,否则删除节点值

///

///

/**************************************************

* 使用示列:

* XmlHelper.Delete(path, "/Node", "")

* XmlHelper.Delete(path, "/Node", "Attribute")

************************************************/

public static void Delete(string path, string node, string attribute)

{

try

{

XmlDocument doc = new XmlDocument();

doc.Load(path);

XmlNode xn = doc.SelectSingleNode(node);

XmlElement xe = (XmlElement)xn;

if (attribute.Equals(""))

xn.ParentNode.RemoveChild(xn);

else

xe.RemoveAttribute(attribute);

doc.Save(path);

}

catch { }

}

}

后来,考虑一下Linq,不就是干这个事情的吗?Linq是从集合中查询对象,在linq to xml中的集合是通过XElement的Elements(),Elements(string name),以及Descendants、DescendantsAndSelf、Ancestors、AncestorsAndSelf的几个重载方法中获得。

获得XElement集合之后,可以通过XElement的Attribute(string name)方法获得元素的属性值,可以通过XElement的Value属性获得节点的文本值;使用linq就可以方便的做查询,做筛选排序了。

我们分析一下这个xml数据结构,

1、首先是取出address节点下面所有的province的name

2、下面取出province下面的所有city,形成集合

3、在取city的时候,取出country集合。

按照这个思路。读取xml数据并形成tree数据结构的Linq如下:

代码如下 复制代码
XmlDocument doc =
new

XmlDocument();
XDocument xdoc = XDocument.Load(context.Server.MapPath(
"/data/Area.xml"
));
var

query =
from

item
in

xdoc.Element(
"address"
).Elements()
select

new
{
name = item.Attribute(
"name"
).Value,
children =
from

cityitem
in

item.Elements()
select

new
{
name = cityitem.Attribute(
"name"
).Value,
children =
from

countryitem
in

cityitem.Elements()
select

new
{
name = countryitem.Attribute(
"name"
).Value
}
}
};

这里用得是XDocument。XDocument和XmlDocument都可以用来操作XML文档,XDocument是.net 3.5为Linq to XML准备的轻量级Document对象,在功能上他和XmlDocument差不多,但是Linq to XML只能配合XDocument使用。XDocument提供了更舒服的创建xml方式。

方便在那里那?


比如我们只取20条记录:

代码如下 复制代码
XmlDocument doc =
new

XmlDocument();
XDocument xdoc = XDocument.Load(context.Server.MapPath(
"/data/Area.xml"
));
var

query =(
from

item
in

xdoc.Element(
"address"
).Elements()
select

new
{
name = item.Attribute(
"name"
).Value,
children =
from

cityitem
in

item.Elements()
select

new
{
name = cityitem.Attribute(
"name"
).Value,
children =
from

countryitem
in

cityitem.Elements()
select

new
{
name = countryitem.Attribute(
"name"
).Value
}
}
}).Take(20);

注意我加了(),并使用Take(20).

原文转自: http://www.*s*uchs*o.com/projecteactual/Csharp-Linq-XDocument-Xml-File-Tree-Data.html 感谢站长

热门栏目