LINQ to XML一个重要的特性是能够方便地修改xml树,如添加、删除、更新和复制xml文档的内容。
I.插入
在下面的示例中,使用AddAfterSelf方法向现有xml中添加一个新节点:
public static void AddAfterSelf()
{
XElement root = XElement.Parse(@"
<Categories>
<Category>
<CategoryID>1</CategoryID>
<CategoryName>Beverages</CategoryName>
<Description>Soft drinks,coffees,teas,beers,and ales</Description>
</Category>
</Categories>
");
XElement xele = root.Element("Category").Element("CategoryName");
xele.AddAfterSelf(new XElement("AddDate",DateTime.Now));
//保存
root.Save("test.xml");
}
运行该示例将会得到一个xml文件,其内容为:
<?xml version="1.0" encoding="utf-8"?> <Categories> <Category> <CategoryID>1</CategoryID> <CategoryName>Beverages</CategoryName> <AddDate>2013-07-22T21:51:42.1550551+08:00</AddDate> <Description>Soft drinks,and ales</Description> </Category> </Categories>当需要添加一个元素到指定节点之前时,可以使用AddBeforeSelf方法。
II.更新
在LINQ to XML中更新xml内容可以使用以下几种方法:
在下面的示例中使用了ReplaceWith与SetElementValue方法对xml进行了更新操作:
public static void Update()
{
XElement root = XElement.Parse(@"
<Categories>
<Category>
<CategoryID>1</CategoryID>
<CategoryName>Beverages</CategoryName>
<Description>Soft drinks,and ales</Description>
</Category>
</Categories>
");
root.Element("Category").Element("CategoryID").ReplaceWith(new XElement("ID","2"));
root.Element("Category").SetElementValue("CategoryName","test data");
root.Save("test.xml");
}
运行该示例将会得到一个xml文件,其内容为:
<?xml version="1.0" encoding="utf-8"?> <Categories> <Category> <ID>2</ID> <CategoryName>test data</CategoryName> <Description>Soft drinks,and ales</Description> </Category> </Categories>
III.删除
可以使用Remove(XElement)与RemoveAll方法来删除xml。
在下面的示例中,使用了RemoveAll方法:
public static void Remove()
{
string path = @"test.xml";
XElement root = XElement.Parse(@"
<Categories>
<Category>
<CategoryID>1</CategoryID>
<CategoryName>Beverages</CategoryName>
<Description>Soft drinks,and ales</Description>
</Category>
</Categories>
");
root.RemoveAll();
root.Save(path);
}
<?xml version="1.0" encoding="utf-8"?> <Categories />在下面的示例中,使用了Remove方法删除了xml的Description元素:
public static void Remove()
{
XElement root = XElement.Parse(@"
<Categories>
<Category>
<CategoryID>1</CategoryID>
<CategoryName>Beverages</CategoryName>
<Description>Soft drinks,and ales</Description>
</Category>
</Categories>
");
root.Element("Category").Element("Description").Remove();
root.Save("test.xml");
}
运行该示例将会得到一个xml文件,其内容为:
<?xml version="1.0" encoding="utf-8"?> <Categories> <Category> <CategoryID>1</CategoryID> <CategoryName>Beverages</CategoryName> </Category> </Categories>
处理属性
I.添加
LINQ to XML添加属性与添加元素师类似的,可以使用构造函数或者Add方法来添加属性:
public static void AddAttribute()
{
XElement root = new XElement("Categories",new XElement("Category",new XAttribute("CategoryID","1"),new XElement("CategoryName","Beverages"),new XElement("Description","Soft drinks,and ales")
)
);
root.Element("Category").Add(new XAttribute("AddDate",DateTime.Now.ToShortDateString()));
root.Save("test.xml");
}
运行该示例将会得到一个xml文件,其内容为:
<?xml version="1.0" encoding="utf-8"?> <Categories> <Category CategoryID="1" AddDate="2013/7/22"> <CategoryName>Beverages</CategoryName> <Description>Soft drinks,and ales</Description> </Category> </Categories>
II.检索
public static void SelectAttribute()
{
XElement root = new XElement("Categories",and ales")
)
);
XAttribute xattr = root.Element("Category").Attribute("CategoryID");
Console.WriteLine(xattr.Name);
Console.WriteLine(xattr.Value);
}
上述代码的运行结果为:
CategoryID
1
原文链接:https://www.f2er.com/xml/300130.html