8,497
社区成员
发帖
与我相关
我的任务
分享
/// <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");