小编Dor*_*sie的帖子

Linq To Xml问题使用XElement的方法Elements(XName)

我使用Linq To Xml时遇到问题.

一个简单的代码.我有这个XML:

<?xml version="1.0" encoding="utf-8" ?>
<data xmlns="http://www.example.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com/directory file.xsd">
<contact>
 <name>aaa</name>
 <email>email@email.ext</email>
 <birthdate>2002-09-22</birthdate>
 <telephone>000:000000</telephone>
 <description>Description for this contact</description>
</contact>
<contact>
 <name>sss</name>
 <email>email@email.ext</email>
 <birthdate>2002-09-22</birthdate>
 <telephone>000:000000</telephone>
 <description>Description for this contact</description>
</contact>
<contact>
 <name>bbb</name>
 <email>email@email.ext</email>
 <birthdate>2002-09-22</birthdate>
 <telephone>000:000000</telephone>
 <description>Description for this contact</description>
</contact>
<contact>
 <name>ccc</name>
 <email>email@email.ext</email>
 <birthdate>2002-09-22</birthdate>
 <telephone>000:000000</telephone>
 <description>Description for this contact</description>
</contact>
Run Code Online (Sandbox Code Playgroud)

我想让每个联系人将它映射到一个对象Contact上.为此,我使用这段代码:

XDocument XDoc = XDocument.Load(System.Web.HttpRuntime.AppDomainAppPath + this.filesource);
XElement XRoot = XDoc.Root;
//XElement XEl = XElement.Load(this.filesource);
var results = from e in XRoot.Elements("contact") 
 select new Contact((string)e.Element("name"), (string)e.Element("email"), "1-1-1", …
Run Code Online (Sandbox Code Playgroud)

c# xelement linq-to-xml elements

4
推荐指数
1
解决办法
8354
查看次数

标签 统计

c# ×1

elements ×1

linq-to-xml ×1

xelement ×1