LINQ to XML 操作XML文档
2009-08-24 15:31
471 查看
LINQ to XML程序集(System.Xml.Linq.dll)在3个不同的命名空间System.Xml.Linq、System.Xml.Schema和System.Xml.XPath。后两者只定义了少数类型。
System.Xml.Linq中有一套可控制的类型,如下:
引入System.Xml.Linq命名空间.
public class _Default
{
/// <summary>
/// XML样式结构
/// </summary>
public void CreateFunctionalXmlElement()
{
XElement inventory = new XElement("Inventory",
new XElement("Car", new XAttribute("ID", "1"),
new XElement("Color", "Green"),
new XElement("PetName", "Stan")
)
);
//
Label1.Text = inventory.ToString();
//TextBox1.Text = inventory.ToString();//??????????????????????????????????????????????????
}
/// <summary>
/// 创建XML文档
/// </summary>
void CreateFunctionalXmlDoc()
{
XDocument inventoryDoc =
new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XComment("fffffffffff"),
new XElement("Inventory",
new XElement("Car", new XAttribute("ID", "1"),
new XElement("Color", "Green"),
new XElement("PetName", "Stan")
),
new XElement("Car1", new XAttribute("ID1", "2"),
new XElement("Color", "red"),
new XElement("PetName", "haha")
)
)
);
string path = Server.MapPath(Request.ApplicationPath);
inventoryDoc.Save(path+"dekunhaha.xml");
}
/// <summary>
/// 从LINQ查询中生成文档
/// </summary>
public void CreateXmlDocFromArray()
{
//创建一个匿名类型数组
var data = new[] {
new{PetName="Melvin",ID=10},
new{PetName="Pat",ID=11},
};
XElement vehicles =
new XElement("Inventory",
from c in data
select new XElement("Car",
new XAttribute("ID",c.ID),
new XElement("PetName",c.PetName)
)
);
Label1.Text = vehicles.ToString();
/*匿名类型例子
*
product[] products=new product[2];
var productQuery =
from prod in products select new { prod.Color, prod.Price };
foreach (var v in productQuery)
{
}
*/
}
/// <summary>
/// 从字符串建立一个XElement
/// </summary>
public void LoadExistingXML()
{
string myElement = @"<Car id='3'><color>Yellow</color><make>Yugo</make></car>";
XElement newXElement = XElement.Parse(myElement);
//加载aa.xml
XDocument myDoc = XDocument.Load("aa.xml");
}
/******************** aa.xml
*
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!--fffffffffff-->
<Inventory>
<Car ID="1">
<Make>Ford</Make>
<Color>Green</Color>
<PetName>Stan</PetName>
</Car>
* <Car1 ID1="2">
<Make>VW</Make>
<Color>red</Color>
<PetName>haha</PetName>
</Car1>
</Inventory>
*/
/// <summary>
/// 选择某一个标签的固定值
/// </summary>
void PrintAllPetNames()
{
XElement xe = new XElement("aa.xml");
var petNames = from pn in xe.Descendants("PetName") select pn.Value; //Stan haha
foreach (var name in petNames)
{
}
var fords = from c in xe.Descendants("Make") where "Ford" == c.Value select c;
}
void updateXML()
{
string path = Server.MapPath(Request.ApplicationPath);
XElement xe =XElement.Load(path + "aa.xml");
//xe.Add(new XElement("Car3", new XAttribute("ID1", "2"),
// new XElement("Color", "red"),
// new XElement("PetName", "haha")
// ));
xe.FirstNode.AddBeforeSelf(new XElement("Car3", new XAttribute("ID1", "2"),
new XElement("Color", "red"),
new XElement("PetName", "haha")
));
var attr = xe.Elements("Car").Attributes("ID1");
foreach (var id in attr)
{
string s = id.Value;
}
//xe.Save(path+"dekunhaha.xml");
}
protected void Button2_Click(object sender, EventArgs e)
{
//long arrayLength = (long)((4.0d) * (11/3.0d));
//long i = arrayLength % 4;
//if (arrayLength % 4 != 0)
//{
// arrayLength += 4 - arrayLength % 4;
//}
//int base64ArraySize = (int)Math.Ceiling(10 / 3d) * 4;
updateXML();
}
}
class product
{
public string Color;
public string Price;
}
System.Xml.Linq中有一套可控制的类型,如下:
成员 | 含义 |
XDocument | 整个XML文档 |
XElement | XML文档中的某个给定元素 |
XDeclaration | XML文档开头的声明 |
XComment | XML注释 |
XAttribute | 代表一个特定XML元素的XML属性 |
XName、XNamespace | 提供了一个简单的方式来定义和引用XML命名空间 |
public class _Default
{
/// <summary>
/// XML样式结构
/// </summary>
public void CreateFunctionalXmlElement()
{
XElement inventory = new XElement("Inventory",
new XElement("Car", new XAttribute("ID", "1"),
new XElement("Color", "Green"),
new XElement("PetName", "Stan")
)
);
//
Label1.Text = inventory.ToString();
//TextBox1.Text = inventory.ToString();//??????????????????????????????????????????????????
}
/// <summary>
/// 创建XML文档
/// </summary>
void CreateFunctionalXmlDoc()
{
XDocument inventoryDoc =
new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XComment("fffffffffff"),
new XElement("Inventory",
new XElement("Car", new XAttribute("ID", "1"),
new XElement("Color", "Green"),
new XElement("PetName", "Stan")
),
new XElement("Car1", new XAttribute("ID1", "2"),
new XElement("Color", "red"),
new XElement("PetName", "haha")
)
)
);
string path = Server.MapPath(Request.ApplicationPath);
inventoryDoc.Save(path+"dekunhaha.xml");
}
/// <summary>
/// 从LINQ查询中生成文档
/// </summary>
public void CreateXmlDocFromArray()
{
//创建一个匿名类型数组
var data = new[] {
new{PetName="Melvin",ID=10},
new{PetName="Pat",ID=11},
};
XElement vehicles =
new XElement("Inventory",
from c in data
select new XElement("Car",
new XAttribute("ID",c.ID),
new XElement("PetName",c.PetName)
)
);
Label1.Text = vehicles.ToString();
/*匿名类型例子
*
product[] products=new product[2];
var productQuery =
from prod in products select new { prod.Color, prod.Price };
foreach (var v in productQuery)
{
}
*/
}
/// <summary>
/// 从字符串建立一个XElement
/// </summary>
public void LoadExistingXML()
{
string myElement = @"<Car id='3'><color>Yellow</color><make>Yugo</make></car>";
XElement newXElement = XElement.Parse(myElement);
//加载aa.xml
XDocument myDoc = XDocument.Load("aa.xml");
}
/******************** aa.xml
*
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!--fffffffffff-->
<Inventory>
<Car ID="1">
<Make>Ford</Make>
<Color>Green</Color>
<PetName>Stan</PetName>
</Car>
* <Car1 ID1="2">
<Make>VW</Make>
<Color>red</Color>
<PetName>haha</PetName>
</Car1>
</Inventory>
*/
/// <summary>
/// 选择某一个标签的固定值
/// </summary>
void PrintAllPetNames()
{
XElement xe = new XElement("aa.xml");
var petNames = from pn in xe.Descendants("PetName") select pn.Value; //Stan haha
foreach (var name in petNames)
{
}
var fords = from c in xe.Descendants("Make") where "Ford" == c.Value select c;
}
void updateXML()
{
string path = Server.MapPath(Request.ApplicationPath);
XElement xe =XElement.Load(path + "aa.xml");
//xe.Add(new XElement("Car3", new XAttribute("ID1", "2"),
// new XElement("Color", "red"),
// new XElement("PetName", "haha")
// ));
xe.FirstNode.AddBeforeSelf(new XElement("Car3", new XAttribute("ID1", "2"),
new XElement("Color", "red"),
new XElement("PetName", "haha")
));
var attr = xe.Elements("Car").Attributes("ID1");
foreach (var id in attr)
{
string s = id.Value;
}
//xe.Save(path+"dekunhaha.xml");
}
protected void Button2_Click(object sender, EventArgs e)
{
//long arrayLength = (long)((4.0d) * (11/3.0d));
//long i = arrayLength % 4;
//if (arrayLength % 4 != 0)
//{
// arrayLength += 4 - arrayLength % 4;
//}
//int base64ArraySize = (int)Math.Ceiling(10 / 3d) * 4;
updateXML();
}
}
class product
{
public string Color;
public string Price;
}
相关文章推荐
- LINQ to XML 操作XML文档
- LINQ to XML 操作XML文档
- LINQ to XML操作Xml文档
- linq to xml操作XML,这是转的大神的,自己学习了,也希望你们也能学习一下
- C# Linq to xml学习 用XElement类读写XML文档(用作配置文档)
- LINQ to XML 操作XML文档
- LINQ to XML实现XML文档的增删改查
- Linq学习笔记---Linq to Xml操作
- 在metro应用中用linq to xml+文件操作生成XML
- linq to xml 基本操作
- 解析linq to xml操作XML的示例分析
- Linq to Xml下类似XML DOM中InnerXml的操作
- 解析linq to xml操作XML的示例分析
- linq to xml 简单的增、删、改、查、保存xml文件操作
- LinqtoXml的基本操作
- LINQ TO XML 基本操作
- asp.net MVC中linq to xml的简单操作
- 用Linq to XML的方式查询XML 文档
- LINQ to XML实现XML文档的增删改查
- 使用Linq to XML操作XML的元素