using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace LinqDBPro.App_Code
{
class XMLOp
{
//xml的读写更新,删除
//1. 生成xml
public void createXml() {
XmlDocument document = new XmlDocument();
XmlElement root = document.CreateElement("Customers");
document.AppendChild(root);
Console.WriteLine("开始生成xml");
for (int i = 0; i < 10;i++ )
{
XmlElement childNode = document.CreateElement("Customer");
childNode.SetAttribute("customerId",i.ToString());
childNode.SetAttribute("customerCode",Guid.NewGuid().ToString());
XmlElement customerNode = document.CreateElement("customerName");
customerNode.InnerText = "company("+(i+1)+")";
XmlElement customerAddressNode = document.CreateElement("customerAddress");
customerAddressNode.InnerText = "深圳"+i;
childNode.AppendChild(customerNode);
childNode.AppendChild(customerAddressNode);
root.AppendChild(childNode);
}
document.Save("d:\\data\\customer.xml");
Console.WriteLine("生成完毕..");
}
//解析Xml
public void readXml() {
XmlDocument document = new XmlDocument();
document.Load("d:\\data\\customer.xml");
XmlNode root = document.SelectSingleNode("Customers"); //取到根节点
XmlNodeList childNodeList = root.ChildNodes; //取到根节点下的所有子节点
foreach( XmlNode childNode in childNodeList ){
XmlElement childElement = childNode as XmlElement;
string customerId = childElement.GetAttribute("customerId");
string customerCode = childElement.GetAttribute("customerCode");
Console.WriteLine(customerId+":"+customerCode);
//取customer下的子节点
XmlNodeList cNodeList = childElement.ChildNodes;
foreach(XmlNode node in cNodeList ){
XmlElement cElement = node as XmlElement;
Console.WriteLine(cElement.Name + ":" + cElement.InnerText);
}
}
}
//更新节点,eg:给每个子节点加一个customerEnglishName
public void updateXml() {
string xmlPath = "d:\\data\\customer.xml";
XmlDocument document = new XmlDocument();
Console.WriteLine("bgin to read xml..");
document.Load(xmlPath);
Console.WriteLine("read xml ok..\n get the root node.");
XmlNode root = document.SelectSingleNode("Customers");
XmlNodeList childList = root.ChildNodes;
foreach (XmlNode node in childList)
{
XmlElement childElement = node as XmlElement;
Console.WriteLine("update node :"+childElement.GetAttribute("customerId"));
XmlElement customerEnglishNameNode = document.CreateElement("customerEnglishName");
customerEnglishNameNode.InnerText = "HuaWei co.ltd";
childElement.AppendChild(customerEnglishNameNode);
}
document.Save(xmlPath);
}
//增加一个新的节点
public void addNewNode(){
string xmlPath = "d:\\data\\customer.xml";
XmlDocument document = new XmlDocument();
document.Load(xmlPath);
XmlNode root = document.SelectSingleNode("Customers");
Console.WriteLine("begin add..");
XmlElement newElement = document.CreateElement("Customer");
newElement.SetAttribute("customerId","10");
newElement.SetAttribute("customerCode",Guid.NewGuid().ToString());
XmlElement newCustomerNameNode = document.CreateElement("customerName");
newCustomerNameNode.InnerText = "newCompany";
XmlElement newCustomerAddressNode = document.CreateElement("customerAddress");
newCustomerAddressNode.InnerText = "xi'an";
XmlElement newCustomerEnglishNode = document.CreateElement("customerEnglishName");
newCustomerEnglishNode.InnerText = "new inc.";
newElement.AppendChild(newCustomerNameNode);
newElement.AppendChild(newCustomerAddressNode);
newElement.AppendChild(newCustomerEnglishNode);
root.AppendChild(newElement);
document.Save(xmlPath);
Console.WriteLine("add ok..");
}
//删除某个节点
public void deleteNode() {
string xmlPath = "d:\\data\\customer.xml";
XmlDocument document = new XmlDocument();
document.Load(xmlPath);
string deleteCustomerCode = "904063aa-17a9-438a-8f3f-a31641fb230b";
XmlNode root = document.SelectSingleNode("Customers");
XmlNodeList nodeList = root.ChildNodes;
foreach(XmlNode node in nodeList ){
Console.WriteLine("begin to search..");
XmlElement childElement = node as XmlElement;
string customerCode = childElement.GetAttribute("customerCode");
if(customerCode.Equals(deleteCustomerCode)){
Console.WriteLine("searched..Id:"+childElement.GetAttribute("customerId"));
root.RemoveChild(childElement);
Console.WriteLine("deleteed..");
}
}
document.Save(xmlPath); } } }
原文链接:https://www.f2er.com/xml/298482.html