1. [代码][C#]代码
\
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace XmlFileTest
{
class Program
{
static void Main(string[] args)
{
bool isCreateXml=false;
string xmlFilePath = @"C:\Users\Administrator\Desktop\myXml.xml";
while (true)
{
Console.WriteLine("输入1:创建xml\n输入2:获取xml信息\n输入3:修改xml\n输入4:添加节点\n输入5:删除节点\n输入6:退出");
string flag = Console.ReadLine();
switch (flag)
{
case "1":
CreateXml(xmlFilePath);
isCreateXml = true;
break;
case "2":
if (isCreateXml)
{
GetXmlInformation(xmlFilePath);
}
else { Console.WriteLine("xml文件尚未创建完成"); }
break;
case "3":
if (isCreateXml)
{
ModifyXmlInformation(xmlFilePath);
Console.WriteLine("xml修改完成,输入2查看\n");
}
else { Console.WriteLine("xml文件尚未创建完成"); }
break;
case "4":
if (isCreateXml)
{
AddXmlInformation(xmlFilePath);
Console.WriteLine("xml节点添加完成,输入2查看\n");
}
else { Console.WriteLine("xml文件尚未创建完成"); }
break;
case "5":
if (isCreateXml)
{
DeleteXmlInformation(xmlFilePath);
Console.WriteLine("xml节点删除完成,输入2查看\n");
}
else { Console.WriteLine("xml文件尚未创建完成"); }
break;
case "6":
return;
default:
break;
}
}
}
public static void CreateXml(string xmlPath)
{
XmlDocument myXml = new XmlDocument();
XmlElement root = myXml.CreateElement("异界小说");
myXml.AppendChild(root);
//根节点
XmlElement bookOne = myXml.CreateElement("琥珀之剑");
bookOne.SetAttribute("作者", "绯炎");
bookOne.SetAttribute("简介", "命运在我眼前分开成两条互不相关笔直的线,");
root.AppendChild(bookOne);
//第一层
XmlElement bookChapter = myXml.CreateElement("章节");
bookChapter.SetAttributeNode("章节数", "二百二十六");
bookOne.AppendChild(bookChapter);
//第二章
XmlElement bookContext1 = myXml.CreateElement("内容");
bookContext1.SetAttribute("第一章", "梦中人");
bookContext1.InnerText = "http://read.qidian.com/BookReader/1784765,30184328.aspx";
bookChapter.AppendChild(bookContext1);
XmlElement bookContext2 = myXml.CreateElement("内容");
bookContext2.SetAttribute("第二章", "苏菲的世界");
bookContext2.InnerText = "http://read.qidian.com/BookReader/1784765,30194232.aspx";
bookChapter.AppendChild(bookContext2);
XmlElement bookContext3 = myXml.CreateElement("内容");
bookContext3.SetAttribute("第三章", "亡灵");
bookContext3.InnerText = "http://read.qidian.com/BookReader/1784765,30197921.aspx";
bookChapter.AppendChild(bookContext3);
XmlElement bookTwo = myXml.CreateElement("海盗系统");
bookTwo.SetAttribute("作者", "正气蛋");
bookTwo.SetAttribute("简介", @"在惊涛怒浪中扬帆远航, 在炮火纷飞中饮酒大笑, 这是海盗的赞歌! ");
root.AppendChild(bookTwo);
XmlElement bookChapter2 = myXml.CreateElement("章节");
bookChapter2.SetAttributeNode("章节数", "五百五十一");
bookTwo.AppendChild(bookChapter2);
XmlElement bookContext4 = myXml.CreateElement("内容");
bookContext4.SetAttribute("第一章", "海盗王");
bookContext4.InnerText = "http://read.qidian.com/BookReader/2123512,34819027.aspx";
bookChapter2.AppendChild(bookContext4);
XmlElement bookContext5 = myXml.CreateElement("内容");
bookContext5.SetAttribute("第二章", "孤岛");
bookContext5.InnerText = "http://read.qidian.com/BookReader/2123512,34824359.aspx";
bookChapter2.AppendChild(bookContext5);
XmlElement bookContext6 = myXml.CreateElement("内容");
bookContext6.SetAttribute("第三章", "残图");
bookContext6.InnerText = "http://read.qidian.com/BookReader/2123512,34833260.aspx";
bookChapter2.AppendChild(bookContext6);
try
{
myXml.Save(xmlPath);
}
catch (Exception ex)
{ Console.WriteLine(ex.Message); }
}
public static void GetXmlInformation(string xmlPath)
{
try
{
XmlDocument myXmlFile = new XmlDocument();
myXmlFile.Load(xmlPath);
XmlNode root = myXmlFile.SelectSingleNode("异界小说");
string innerXml = root.InnerXml.ToString();
string outXml = root.OuterXml.ToString();
Console.WriteLine(innerXml+'\n');//带根节点输出
Console.WriteLine(outXml + '\n');//不带根节点输出
XmlNodeList nodeList = root.ChildNodes;
foreach (XmlNode xmlNode in nodeList)
{
XmlAttributeCollection xmlAttrColl = xmlNode.Attributes;
foreach (XmlAttribute xmlAttr in xmlAttrColl)
{
string name = xmlAttr.Name;
string value = xmlAttr.Value;
Console.WriteLine("{0} = {1}", name, value);
}
if (xmlNode.HasChildNodes)
{
XmlNode secNode = xmlNode.FirstChild;
XmlAttributeCollection secXmlAttrColl = secNode.Attributes;
foreach (XmlAttribute xmlAttr in secXmlAttrColl)
{
string name = xmlAttr.Name;
string value = xmlAttr.Value;
Console.WriteLine("{0} = {1}", name, value);
}
if (secNode.HasChildNodes)
{
foreach (XmlNode node in secNode)
{
XmlNode thirdNode = node;
XmlAttributeCollection thirdXmlAttrColl = thirdNode.Attributes;
foreach (XmlAttribute xmlAttr in thirdXmlAttrColl)
{
string name = xmlAttr.Name;
string value = xmlAttr.Value;
Console.WriteLine("{0} = {1}", name, value);
}
string innerText = node.InnerText;
Console.WriteLine(innerText);
}
}
}
}
//逐条输出
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public static void ModifyXmlInformation(string xmlPath)
{
try
{
XmlDocument myXmlFile = new XmlDocument();
myXmlFile.Load(xmlPath);
XmlNode root = myXmlFile.FirstChild;
XmlNodeList nodeList = root.ChildNodes;
foreach (XmlNode firNode in nodeList)
{
if (firNode.Attributes["简介"].Value.Equals("命运在我眼前分开成两条互不相关笔直的线,"))
{
firNode.Attributes["简介"].Value = "已经成功改变了值";
}
if (firNode.Attributes["简介"].Value.Equals(@"在惊涛怒浪中扬帆远航, 在炮火纷飞中饮酒大笑, 这是海盗的赞歌! "))
{
firNode.Attributes["简介"].Value = "已经成功改变了值";
}
}
myXmlFile.Save(xmlPath);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public static void DeleteXmlInformation(string xmlPath)
{
try
{
XmlDocument myXmlFile = new XmlDocument();
myXmlFile.Load(xmlPath);
XmlNode root = myXmlFile.FirstChild;
XmlNodeList nodeList = root.ChildNodes;
foreach (XmlNode firNode in nodeList)
{
if (firNode.HasChildNodes)
{
foreach (XmlNode secNode in firNode)
{
XmlNode lastNode = secNode.LastChild;
lastNode.RemoveAll();
secNode.RemoveChild(lastNode);
}
}
}
myXmlFile.Save(xmlPath);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public static void AddXmlInformation(string xmlPath)
{
try
{
XmlDocument myXmlFile = new XmlDocument();
myXmlFile.Load(xmlPath);
XmlNode root = myXmlFile.FirstChild;
XmlNodeList nodeList = root.ChildNodes;
foreach (XmlNode firNode in nodeList)
{
if (firNode.HasChildNodes)
{
foreach (XmlNode secNode in firNode)
{
XmlElement newElement = myXmlFile.CreateElement("长文章");
newElement.SetAttribute("字数", "100w以上");
secNode.AppendChild(newElement);
}
}
}
myXmlFile.Save(xmlPath);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}