欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > asp.net >内容正文

asp.net

LINQ TO XML练习

发布时间:2025/5/22 asp.net 38 豆豆
生活随笔 收集整理的这篇文章主要介绍了 LINQ TO XML练习 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

最近要用到LINQ,以前没用到过,进入linq的学习中............

下面是我朋友给的练习

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Text;

namespace ConsoleApplication3
{
class Program
{

static void Main(string[] args)
{
Dictionary
<string, string> xDic = new Dictionary<string, string>();
XDocument xDoc
= XDocument.Load("D:\\Example.xml");
XElement xEle
= xDoc.Root;
IEnumerable
<XElement> xEnum = xEle.Elements();
foreach (XElement xEleJSCommand in xEnum)
{
string strKey = "JS:";
strKey
+= xEleJSCommand.Attribute("xmlns").Value.ToString()+".";
IEnumerable
<XElement> xEleCom = xEleJSCommand.Elements();
foreach (XElement xEleCommand in xEleCom)
{
strKey
+= xEleCommand.Attribute("name").Value.ToString();
xDic.Add(strKey, xEleCommand.Value.ToString());
}
}
foreach(KeyValuePair<string,string> kevp in xDic)
{
Console.WriteLine(
"Key:{0}\nvalue:{1}", kevp.Key, kevp.Value);
}
Console.ReadKey();
}
}
}

总结

以上是生活随笔为你收集整理的LINQ TO XML练习的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。