linq to xml 的问题

maxmax1986 2012-05-09 04:26:54
小弟对linq to xml还是菜鸟,望高手给予解答,现有这么一段xml代码,要把这里面的信息填充到一个class societe中,对linq的语法不熟悉,写了半天没弄明白,xml中多余的项,不在类中的项可以不要
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE main>
-<main> <no type="siret">44373969300010</no> <deno>AXONES</deno> <enseigne/> <adresse>205 Avenue Georges Clemenceau - Immeuble le Clemenceau</adresse> <codepostal>92024</codepostal> <commune>NANTERRE CEDEX</commune> <ape>6201Z</ape> <apetexte>Programmation informatique</apetexte> <typeetab>siège</typeetab><url>http://www.societe.com/societe/axones-443739693.html</url> </main>


class Societe
{
public string nosiret { get; set; }
public string deno { get; set; }
public string adresse { get; set; }
public string codepostal { get; set; }
public string commune { get; set; }
public string struri { get; set; }
}
...全文
56 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
你的选择B 2012-05-09
  • 打赏
  • 举报
回复
XDocument doc = XDocument.Load(@"F:\Data.xml");
var v = from u in doc.Descendants("main")
select new Societe
{
deno = u.Element("deno").Value,
adresse = u.Element("adresse").Value,
codepostal = u.Element("codepostal").Value,
commune = u.Element("commune").Value
};
foreach (var ss in v)
{
richTextBox1.Text += ss.deno + "\n";
richTextBox1.Text += ss.adresse + "\n";
richTextBox1.Text += ss.codepostal + "\n";
richTextBox1.Text += ss.commune + "\n";

richTextBox1.Text += "\n";
}
maxmax1986 2012-05-09
  • 打赏
  • 举报
回复
发完帖子后来又看了会就搞定了,还是谢谢你了,就等着来个人回答下,结贴了 呵呵
[Quote=引用 1 楼 的回复:]

C# code

void Main()
{

XDocument data=XDocument.Load(@"c:\test.xml");
var query=from item in data.Descendants("main")
select new Societe
{
d……
[/Quote]
q107770540 2012-05-09
  • 打赏
  • 举报
回复

void Main()
{

XDocument data=XDocument.Load(@"c:\test.xml");
var query=from item in data.Descendants("main")
select new Societe
{
deno=item.Element("no").Value,
struri=item.Element("url").Value,
//以下你懂的
};
Console.WriteLine(query);
}
class Societe
{
public string nosiret { get; set; }
public string deno { get; set; }
public string adresse { get; set; }
public string codepostal { get; set; }
public string commune { get; set; }
public string struri { get; set; }
}

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧