這幾天想寫點基礎的博客內容,正好最近在用XML,就寫了一點XML的基本操作。
通過函數的形式表現出來。
/*
-------------------------------------
Context:LINQ TO XML
-------------------------------------
Author:Chinajiyong
-------------------------------------
DateTime:2012-04-21
-------------------------------------
通過函數的形式表現出來
-------------------------------------
*/
/// <summary>
/// 1、創建BookStore.xml的XML文件,由函數CreateXmlFile()完成
/// </summary>
/// <param name="xmlpath">XML文件的路徑</param>
private static void CreateXmlFile(string xmlpath)
{
XDocument doc = new XDocument( ///創建XDocument類的實例
new XDeclaration("1.0", "utf-8", "yes"),///XML的聲明,包括版本,編碼,xml文件是否獨立
new XElement("Books", ///添加根節點
new XElement("Book", ///添加一個節點
new XAttribute("BookID", "001"),///添加屬性BookID
new XElement("BookNo", "0001"), ///添加元素BookNo
new XElement("BookName", "Book 0001"),///添加元素BookName
new XElement("BookPrice", "40"),///添加元素BookPrice
new XElement("BookRemark", "This is a book 0001")///添加元素BookRemark
)
)
);
///保存XML文件到指定地址
doc.Save(xmlpath);
Console.WriteLine(doc);
}
調用函數CreateXmlFile(string xmlpath) static void Main(string[] args)
{
///創建一個名爲BookStore.xml的xml文件
Program.CreateXmlFile(@"C:\BookStore.xml");
}
運行結果:
/// <summary>
/// 2、添加元素
/// </summary>
/// <param name="xmlpath">XML文件的路徑</param>
private static void AddXmlElement(string xmlpath)
{
///導入XML文件
XElement xe = XElement.Load(xmlpath);
///創建一個新節點
XElement book1 = new XElement("Book",
new XAttribute("BookID", "002"),
new XElement("BookNo", "0002"),
new XElement("BookName", "Book 0002"),
new XElement("BookPrice", "50"),
new XElement("BookRemark", "This is a book 0002")
);
///添加節點到XML文件中,並保存
xe.Add(book1);
///創建一個新節點
XElement book2 = new XElement("Book",
new XAttribute("BookID", "003"),
new XElement("BookNo", "0003"),
new XElement("BookName", "Book 0003"),
new XElement("BookPrice", "30"),
new XElement("BookRemark", "This is a book 0003")
);
///添加節點到XML文件中,並保存
xe.Add(book2);
///創建一個新節點
XElement book3 = new XElement("Book",
new XAttribute("BookID", "004"),
new XElement("BookNo", "0004"),
new XElement("BookName", "Book 0004"),
new XElement("BookPrice", "60"),
new XElement("BookRemark", "This is a book 0004")
);
///添加節點到XML文件中
xe.Add(book3);
///保存到XML文件中
xe.Save(xmlpath);
Console.WriteLine(xe);
}
調用函數AddXmlElement(string xmlpath)
///添加XML元素
Program.AddXmlElement(@"C:\BookStore.xml");
運行結果:
/// <summary>
/// 3、修改XML文件的元素
/// </summary>
/// <param name="xmlpath">XML文件的路徑</param>
/// <param name="strElement">指定的修改元素</param>
private static void ModifyXmlElement(string xmlpath, string strElement)
{
XElement xe = XElement.Load(xmlpath);
///查詢修改的元素
IEnumerable<XElement> element = from e in xe.Elements("Book")
where e.Attribute("BookID").Value == strElement
select e;
///修改元素
if (element.Count() > 0)
{
XElement firstelement = element.First();
///設置新的屬性
firstelement.SetAttributeValue("BookID", "new004");
///替換成新的節點
firstelement.ReplaceNodes(
new XElement("BookNo", "new0004"),
new XElement("BookName", "Book new0004"),
new XElement("BookPrice", "45"),
new XElement("BookRemark", "This is a book new0004")
);
}
xe.Save(xmlpath);
Console.WriteLine(xe);
}
調用函數ModifyXmlElement(string xmlpath, string strElement)
///修改XML文件的元素
Program.ModifyXmlElement(@"C:\BookStore.xml", "004");
運行結果:
/// <summary>
/// 4、刪除XML文件的元素
/// </summary>
/// <param name="xmlpath">XML文件的路徑</param>
/// <param name="strElement">指定刪除元素</param>
private static void DeleteXmlElement(string xmlpath, string strElement)
{
XElement xe = XElement.Load(xmlpath);
///查詢修改的元素
IEnumerable<XElement> element = from e in xe.Elements("Book")
where e.Attribute("BookID").Value == strElement
select e;
///修改元素
if (element.Count() > 0)
{
XElement firstelement = element.First();
///刪除此元素的所有節點和屬性
firstelement.RemoveAll();
///刪除此元素的屬性
//firstelement.RemoveAttributes();
///刪除此元素的子節點
//firstelement.RemoveNodes();
}
xe.Save(xmlpath);
Console.WriteLine(xe);
}
調用函數 DeleteXmlElement(string xmlpath, string strElement) ///刪除XML元素
Program.DeleteXmlElement(@"C:\BookStore.xml","new004");
運行結果:
/// <summary>
/// 5、將XML文件中的屬性更換成元素
/// </summary>
/// <param name="xmlpath">XML文件的路徑</param>
/// <param name="strAttribute">指定要更換的屬性</param>
private static void ConvertAttributeToElement(string xmlpath, string strAttribute)
{
XElement xe = XElement.Load(xmlpath);
///查詢更換的元素
IEnumerable<XElement> element = from e in xe.Elements("Book")
where e.Attribute("BookID").Value == strAttribute
select e;
///更換爲元素
if (element.Count() > 0)
{
XElement firstelement = element.First();
//獲取第一個屬性
XAttribute attr = firstelement.FirstAttribute;
//XAttribute attr = firstelement.Attribute("BookID");
///將屬性轉換成元素
firstelement.AddFirst(
new XElement(attr.Name, attr.Value)//添加BookID元素
);
///刪除屬性
firstelement.RemoveAttributes();
}
xe.Save(xmlpath);
Console.WriteLine(xe);
}
調用函數:ConvertAttributeToElement(string xmlpath, string strAttribute) ///刪除XML元素
//Program.DeleteXmlElement(@"C:\BookStore.xml","new004");//註釋這一樣
///將文件中的屬性更換成元素
Program.ConvertAttributeToElement(@"C:\BookStore.xml","new004");
運行結果:
/// <summary>
/// 6、查詢根元素
/// </summary>
/// <param name="xmlpath">XML文件的路徑</param>
private static void QueryRootElement(string xmlpath)
{
XDocument doc = XDocument.Load(xmlpath);
Console.WriteLine(doc.Root.Name);
//IEnumerable<XElement> RootElement = from root in doc.Elements("Books")
// select root;
/////輸出根元素
//foreach (XElement xe in RootElement)
//{
// Console.WriteLine(xe.Name);
//}
}
調用函數:QueryRootElement(string xmlpath)
/// 查詢根元素 Program.QueryRootElement(@"C:\BookStore.xml");運行結果:
Books
/// <summary>
/// 7、查詢指定名稱的元素
/// </summary>
/// <param name="xmlpath">XML文件的路徑</param>
/// <param name="strName">查詢元素名稱</param>
private static void QueryElementByName(string xmlpath, string strName)
{
XElement xe = XElement.Load(xmlpath);
///查詢元素
var elements = xe.Elements("Book")
.Where(e => (string)e.Element("BookName") == strName)
.OrderBy(e => e.Element("BookName"))
.ToList();
elements.ForEach(e => Console.WriteLine(e));
}
調用函數:QueryElementByName(string xmlpath, string strName) ///查詢指定名稱的元素
Program.QueryElementByName(@"C:\BookStore.xml", "Book 0003");
運行結果:
/// <summary>
/// 8、查詢指定屬性的元素
/// </summary>
/// <param name="xmlpath">XML文件的路徑</param>
/// <param name="strName">指定的屬性</param>
private static void QueryElementByAttribute(string xmlpath, string strAttribute)
{
XElement xe = XElement.Load(xmlpath);
///查詢元素
var eAttribute = xe.Elements("Book")
.Where(e => (string)e.Attribute("BookID") == strAttribute)
.OrderBy(e => e.Element("BookID"))
.ToList();
eAttribute.ForEach(e => Console.WriteLine(e));
}
調用函數:QueryElementByAttribute(string xmlpath, string strAttribute) ///查詢指定屬性的元素
Program.QueryElementByAttribute(@"C:\BookStore.xml", "003");
運行結果:
/// <summary>
/// 9、查詢指定元素的子元素
/// </summary>
/// <param name="xmlpath">XML文件的路徑</param>
/// <param name="strSubElement">指定元素</param>
private static void QuerySubElement(string xmlpath, string strSubElement)
{
XElement xe = XElement.Load(xmlpath);
var elements = xe.Elements("Book")
.Descendants("BookRemark")
.ToList();
foreach (var e in elements)
{
Console.WriteLine(e.Name.LocalName + "\t" + e.Value);
}
}
調用函數:QuerySubElement(string xmlpath, string strSubElement)
///查詢指定元素的子元素
Program.QuerySubElement(@"C:\BookStore.xml", "BookRemark");
運行結果:
/// <summary>
/// 10、查詢元素並排序
/// </summary>
/// <param name="xmlpath">XML文件的路徑</param>
private static void QueryElementByOrder(string xmlpath)
{
XElement xe = XElement.Load(xmlpath);
var elements = xe.Elements("Book")
.Where(e => Convert.ToInt32(e.Attribute("BookID").Value.Substring(e.Attribute("BookID").Value.Length - 1, 1)) > 1)
.OrderByDescending(e => (string)e.Element("BookName"))
.ToList();
elements.ForEach(e => Console.WriteLine(e.Element("BookName").Value));
}
調用函數:QueryElementByOrder(string xmlpath) /// 查詢元素並排序
Program.QueryElementByOrder(@"C:\BookStore.xml");
運行結果:
/// <summary>
/// 11、查詢元素並計算Price平均值
/// </summary>
/// <param name="xmlpath">XML文件的路徑</param>
private static void QueryElementByCoeompute(string xmlpath)
{
XElement xe = XElement.Load(xmlpath);
var elements = xe.Elements("Book")
.Where(e => Convert.ToInt32(e.Attribute("BookID").Value.Substring(e.Attribute("BookID").Value.Length - 1, 1)) > 1)
.OrderByDescending(e => (string)e.Element("BookName"))
.ToList();
Console.WriteLine("Average:" + elements.Average(e => Convert.ToInt32(e.Element("BookPrice").Value)));
}
調用函數:QueryElementByCoeompute(string xmlpath) /// 查詢元素並計算Price平均值
Program.QueryElementByCoeompute(@"C:\BookStore.xml");
運行結果:
到此爲止全部操作已經完成。整個操作仍然很簡單。