您的位置:首页 > 其它

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中有一套可控制的类型,如下:

成员含义
XDocument整个XML文档
XElementXML文档中的某个给定元素
XDeclarationXML文档开头的声明
XCommentXML注释
XAttribute代表一个特定XML元素的XML属性
XName、XNamespace提供了一个简单的方式来定义和引用XML命名空间
引入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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: