string bfilePath = @"E:\111" ;
if (Directory.Exists(bfilePath) == false)
{
MessageBox.Show("指定文件夾不存在,請重新選擇路徑!");
return;
}
try
{
string title = string.Empty;
string author = string.Empty;
//讀取XML文檔
XmlDocument xmlDoc = new XmlDocument();
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreComments = true;//忽略文档里面的注释
XmlReader reader = XmlReader.Create(@"E:\111\Book2.xml", settings);
xmlDoc.Load(reader);
string books = xmlDoc.SelectSingleNode("bookstore").InnerText; //SelectSingleNode()得到指定的结点 節點內的值
// 得到根节点bookstore
XmlNode Con_Information = xmlDoc.SelectSingleNode("bookstore");
// 得到根节点的所有子节点
XmlNodeList Con_Info_List = Con_Information.ChildNodes;
foreach (XmlNode xn1 in Con_Info_List)
{
title = xn1.SelectSingleNode("title").InnerText;
author = xn1.SelectSingleNode("author").InnerText;
}
//生成XML文檔
XmlDocument wxmlDoc = new XmlDocument();
XmlDeclaration declaration1 = wxmlDoc.CreateXmlDeclaration("1.0", "UTF-8", "");//xml文档的声明部分
wxmlDoc.AppendChild(declaration1);
XmlElement el = wxmlDoc.CreateElement("books"); //根元素
el.SetAttribute("xmlns", @"http://xmlns.qualcomm.com/it/eng/stargate/assemblyData/v1");
el.SetAttribute("xmlns:xsi", @"http://www.w3.org/2001/XMLSchema-instance");
el.SetAttribute("version", "1.2");
XmlAttribute xaa = wxmlDoc.CreateAttribute("schemaLocation", "http://www.w3.org/2001/XMLSchema-instance");
xaa.Value = "http://xmlns.qualcomm.com/it/eng/stargate/assemblyData/v1 /qualcomm/it/eng/stargate/assemblyData/v1/AssemblyData-1_2.xsd";
xaa.Prefix = "xsi";
el.Attributes.Append(xaa);
wxmlDoc.AppendChild(el);
XmlElement head = wxmlDoc.CreateElement("header"); //子元素第一部分
el.AppendChild(head);
XmlElement Supplier = wxmlDoc.CreateElement("Supplier");
Supplier.InnerText = "FALCON";
head.AppendChild(Supplier);
XmlElement booker = wxmlDoc.CreateElement("booker"); //子元素 第二部分
el.AppendChild(booker);
#region
//第二部分 子元素 的子元素
XmlElement book = wxmlDoc.CreateElement("book1");
booker.AppendChild(book);
XmlElement title1 = wxmlDoc.CreateElement("title"); // <title>路由型与交换型互联网基础</title>
title1.InnerText = "路由型与交换型互联网基础";
book.AppendChild(title1);
XmlElement author1 = wxmlDoc.CreateElement("author"); // <author>程庆梅</author>
title1.InnerText = "程庆梅";
book.AppendChild(title1);
// <TestDateTime start="1111" end="222" timeZone="Asia/Shanghai" />
XmlElement TestDateTime = wxmlDoc.CreateElement("TestDateTime");
TestDateTime.SetAttribute("start", "1111");
TestDateTime.SetAttribute("end", "222");
TestDateTime.SetAttribute("timeZone", "Asia/Shanghai");
book.AppendChild(TestDateTime);
#endregion
string newFilePath = @"E:\111\WBook.xml";
if (File.Exists(newFilePath))
{
File.Delete(newFilePath);
}
wxmlDoc.Save(newFilePath);
}
catch
{
}