首页 > 代码库 > 关于C#读取xml小例子

关于C#读取xml小例子

1.首先这是一个xml文件
<?xml version="1.0" encoding="utf-8"?>
<Document>
<Events>
<Event Name="SalesWareHouseOut" MainAction="WareHouseOut">
<ActionMaping>
<Action Name="WareHouseOut">
<ActionData>Code</ActionData>
<ActionData>CorpOrderID</ActionData>
<ActionData>Actor</ActionData>
<ActionData>ActDate</ActionData>
</Action>
</ActionMaping>
<DataMaping>
<MetaData Name="Code" Type="string" />
<MetaData Name="CorpOrderID" Type="string" />
<MetaData Name="Actor" Type="string" />
<MetaData Name="ActDate" Type="Date" />
</DataMaping>
<DataField>
<Data Code="81143900028040251957" ReplaceCode="" CorpOrderID="138204" Actor="1" ActDate="2013-12-19 08:47:09" WrongCode="" UpperCorpOrderID="" />
<Data Code="81143900027511806124" ReplaceCode="" CorpOrderID="138204" Actor="1" ActDate="2013-12-19 08:47:09" WrongCode="" UpperCorpOrderID="" />
<Data Code="81143900027510453880" ReplaceCode="" CorpOrderID="138204" Actor="1" ActDate="2013-12-19 08:47:09" WrongCode="" UpperCorpOrderID="" />
<Data Code="81143900027509255484" ReplaceCode="" CorpOrderID="138204" Actor="1" ActDate="2013-12-19 08:47:09" WrongCode="" UpperCorpOrderID="" />
<Data Code="81143900027508057635" ReplaceCode="" CorpOrderID="138204" Actor="1" ActDate="2013-12-19 08:47:09" WrongCode="" UpperCorpOrderID="" />
<Data Code="81143900027508204343" ReplaceCode="" CorpOrderID="138204" Actor="1" ActDate="2013-12-19 08:47:09" WrongCode="" UpperCorpOrderID="" />
<Data Code="81143900027509403677" ReplaceCode="" CorpOrderID="138204" Actor="1" ActDate="2013-12-19 08:47:09" WrongCode="" UpperCorpOrderID="" />
<Data Code="81143900027510601766" ReplaceCode="" CorpOrderID="138204" Actor="1" ActDate="2013-12-19 08:47:09" WrongCode="" UpperCorpOrderID="" />
<Data Code="81143900027511950686" ReplaceCode="" CorpOrderID="138204" Actor="1" ActDate="2013-12-19 08:47:09" WrongCode="" UpperCorpOrderID="" />
<Data Code="81143900028040405694" ReplaceCode="" CorpOrderID="138204" Actor="1" ActDate="2013-12-19 08:47:09" WrongCode="" UpperCorpOrderID="" />
</DataField>
</Event>
</Events>
</Document>
如何获取到所有的CorpOrderID
using System.Xml.Linq;//引用这个命名空间
XElement xml = XElement.Load(@"D:\Test\user.xml");//加载这个xml
var query = xml.Descendants("Data");//获取Data节点
foreach (var item in query)
{
    Console.WriteLine(item.Attribute("CorpOrderID").Value);//获取属性为CorpOrderID的值.
}

关于C#读取xml小例子