linq to xml (linq查询)
来源:互联网 发布:csbte武器数据修改 编辑:程序博客网 时间:2024/05/16 05:56
public partial class QueryXML : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
///ReadXmlFile();
///QueryRootElement();
///QueryElementByName();
///QueryElementByAttribute();
///QueryElementBySub();
///QueryElementByOrder();
///QueryElementByCompute();
}
private void ReadXmlFile()//读取XML文件
{ ///导入XML文件
string xmlFilePath = Server.MapPath("Data/Books.xml");
XElement xe = XElement.Load(xmlFilePath);
///显示XML文件的内容
Response.Write(xe);
///设置网页显示的形式为XML文件
Response.ContentType = "text/xml";
Response.End();
}
private void QueryRootElement()//查询根元素
{ ///导入XML文件
string xmlFilePath = Server.MapPath("Data/Books.xml");
XDocument doc = XDocument.Load(xmlFilePath);
///查询根元素
IEnumerable<XElement> elements = from e in doc.Elements("Books")
select e;
///输出根元素的名称
foreach (XElement xe in elements)
{
Response.Write(xe.Name + "<br />");
}
}
private void QueryElementByName()//查询指点定名称的元素
{ ///导入XML文件
string xmlFilePath = Server.MapPath("Data/Books.xml");
XElement xes = XElement.Load(xmlFilePath);
///查询元素
IEnumerable<XElement> elements = from e in xes.Elements("Book")
where (string)e.Element("Name") == "Book 0002"
select e;
///输出元素的ID属性的值
foreach (XElement xe in elements)
{
Response.Write(xe.Name.LocalName + ":" + xe.Attribute("ID").Value + "<br />");
}
}
private void QueryElementByAttribute()//查询指定属性的元素
{ ///导入XML文件
string xmlFilePath = Server.MapPath("Data/Books.xml");
XElement xes = XElement.Load(xmlFilePath);
///查询元素
IEnumerable<XElement> elements = from e in xes.Elements("Book")
where e.Attribute("ID").Value.IndexOf("10") > -1
select e;
///输出元素的ID属性的值
foreach (XElement xe in elements)
{
Response.Write(xe.Name.LocalName + ":" + xe.Attribute("ID").Value + "<br />");
}
}
private void QueryElementBySub()//查询指定元素的子元素
{ ///导入XML文件
string xmlFilePath = Server.MapPath("Data/Books.xml");
XElement xes = XElement.Load(xmlFilePath);
///查询元素
IEnumerable<XElement> elements = from e in xes.Descendants("Remark")
select e;
///输出元素的值
foreach (XElement xe in elements)
{
Response.Write(xe.Name.LocalName + ":" + xe.Value + "<br />");
}
}
private void QueryElementByOrder()//查询元素并排列
{ ///导入XML文件
string xmlFilePath = Server.MapPath("Data/Books.xml");
XElement xes = XElement.Load(xmlFilePath);
///查询元素
IEnumerable<XElement> elements = from e in xes.Elements("Book")
where e.Attribute("ID").Value.IndexOf("10") > -1
orderby (string)e.Element("Name") descending
select e;
///输出元素的名称
foreach (XElement xe in elements)
{
Response.Write(xe.Element("Name").ToString() + "<br />");
}
}
private void QueryElementByCompute()//查询元素并计算指定的值
{ ///导入XML文件
string xmlFilePath = Server.MapPath("Data/Books.xml");
XElement xes = XElement.Load(xmlFilePath);
///查询根元素
IEnumerable<XElement> elements = from e in xes.Elements("Book")
where e.Attribute("ID").Value.IndexOf("10") > -1
orderby (string)e.Element("Name") descending
select e;
///计算价格的平均值
decimal avg = elements.Average(e => decimal.Parse(e.Element("Price").Value));
Response.Write("Average:" + avg.ToString() + "<br />");
}
}
book.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<Books>
<Book ID="101">
<No>00001</No>
<Name>Book 0001</Name>
<Price>100</Price>
<Remark>This is a book 00001.</Remark>
</Book>
<Book ID="102">
<No>00002</No>
<Name>Book 0002</Name>
<Price>200</Price>
<Remark>This is a book 00002.</Remark>
</Book>
<Book>
<ID>106</ID>
<No>0006</No>
<Name>Book 0006</Name>
<Price>600</Price>
<Remark>This is a book 0006.</Remark>
</Book>
</Books>
上面只是对简单xml的操作,如果复杂的xml,原理一样,但是做起来就不是这么简单了,所以对xml的操作,本人还是不建议用linq,相比较看我觉得xpath要简单很多!
- linq to xml (linq查询)
- LINQ TO XML(查询)
- linq to xml(查询)
- LINQ系列:LINQ to XML查询
- LINQ系列:LINQ to XML查询
- LINQ体验(二)LINQ to XML
- 一、Linq to XML (创建,添加,查询)
- Linq To Xml 备忘录3(使用Namespace的查询)
- LINQ TO XML查询结果null 解决方法
- 利用Linq to xml查询html
- Linq TO XML (读写)
- LINQ查询XML字段
- LINQ to Entities查询
- Linq to DataSet查询
- (LINQ 学习系列)(2)LINQ to SQL 查询
- LINQ 查询 Web Service(LINQ to Bing)
- 使用LINQ to XML
- LINQ TO XML
- 安裝程序乱码的解决办法
- special audio in AVI demuxer
- 佛家四大经典爱情故事
- 在retrieve后自动触发rowfocuschanged
- Qt写Excel文件 (转载)
- linq to xml (linq查询)
- Stand-alone Profiler for VS2005
- multi 前缀在计算机中的意义
- 用Linq to XML的方式查询XML 文档
- 给Flex的PopUpManager显示控件添加特效
- 【数论 / 数值】【RQNOJ】羽毛
- 网站测试基本方法-11. 压力测试
- 改进 jQuery Select(单选) 模拟插件 V1.3.6,增加mouseover事件
- Ext Treepanl 加载数据时在IE 7.0版本中报错的解决办法