操作XML
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication2
{
class OperateXML
{
XmlDocument xmldoc;
XmlNode xmlnode;
XmlElement xmlelem;
public void CreateDocByDoc()
{
xmldoc = new XmlDocument();
XmlDeclaration xDecl = xmldoc.CreateXmlDeclaration("1.0", "Unicode", null);
xmldoc.AppendChild(xDecl);
xmlelem = xmldoc.CreateElement("Employees");
xmldoc.AppendChild(xmlelem);
for (int i = 0; i < 3; i++)
{
XmlNode root = xmldoc.SelectSingleNode("Employees");
XmlElement xe1 = xmldoc.CreateElement("Node");
xe1.SetAttribute("genre", "李赞红");
xe1.SetAttribute("ISBN", "2-3631-4");
XmlElement xesub1 = xmldoc.CreateElement("title");
xesub1.InnerText = "CS从入门到精通";
xe1.AppendChild(xesub1);
XmlElement xesub2 = xmldoc.CreateElement("author");
xesub2.InnerText = "候捷";
xe1.AppendChild(xesub2);
XmlElement xesub3 = xmldoc.CreateElement("price");
xesub3.InnerText = "58.3";
xe1.AppendChild(xesub3);
root.AppendChild(xe1);
}
xmldoc.Save("UserInfo1.xml");
}
public void CreateDocByXMLWriter()
{
XmlTextWriter xmlWriter;
string strFilename = "UserInfo2.xml";
xmlWriter = new XmlTextWriter(strFilename, Encoding.Unicode);//创建一个xml文档
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("Employees");
xmlWriter.WriteStartElement("Node");
xmlWriter.WriteAttributeString("genre", "李赞红");
xmlWriter.WriteAttributeString("ISBN", "2-3631-4");
xmlWriter.WriteStartElement("title");
xmlWriter.WriteString("CS从入门到精通");
xmlWriter.WriteEndElement();
xmlWriter.WriteStartElement("author");
xmlWriter.WriteString("候捷");
xmlWriter.WriteEndElement();
xmlWriter.WriteStartElement("price");
xmlWriter.WriteString("58.3");
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
xmlWriter.Close();
}
public void AddNode()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("UserInfo1.xml");
XmlNode root = xmlDoc.SelectSingleNode("Employees");//查找<Employees>
XmlElement xe1 = xmlDoc.CreateElement("Node");//创建一个<Node>节点
xe1.SetAttribute("genre", "张三");//设置该节点genre属性
xe1.SetAttribute("ISBN", "1-1111-1");//设置该节点ISBN属性
XmlElement xesub1 = xmlDoc.CreateElement("title");
xesub1.InnerText = "C#入门帮助";//设置文本节点
xe1.AppendChild(xesub1);//添加到<Node>节点中
XmlElement xesub2 = xmlDoc.CreateElement("author");
xesub2.InnerText = "高手";
xe1.AppendChild(xesub2);
XmlElement xesub3 = xmlDoc.CreateElement("price");
xesub3.InnerText = "158.3";
xe1.AppendChild(xesub3);
root.AppendChild(xe1);//添加到<Employees>节点中
xmlDoc.Save("UserInfo1.xml");
}
public void EditDocData()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("UserInfo1.xml");
XmlNodeList nodeList = xmlDoc.SelectSingleNode("Employees").ChildNodes;//获取Employees节点的所有子节点
foreach (XmlNode xn in nodeList)//遍历所有子节点
{
XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型
if (xe.GetAttribute("genre") == "张三")//如果genre属性值为“张三”
{
xe.SetAttribute("genre", "update张三");//则修改该属性为“update张三”
XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点
foreach (XmlNode xn1 in nls)//遍历
{
XmlElement xe2 = (XmlElement)xn1;//转换类型
if (xe2.Name == "author")//如果找到
{
xe2.InnerText = "亚胜";//则修改
}
}
}
}
xmlDoc.Save("UserInfo1.xml");//保存。
}
public void EditDocStructure()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("UserInfo1.xml");
XmlNodeList nodeList = xmlDoc.SelectSingleNode("Employees").ChildNodes;//获取Employees节点的所有子节点
foreach (XmlNode xn in nodeList)
{
XmlElement xe = (XmlElement)xn;
xe.SetAttribute("test", "111111");
XmlElement xesub = xmlDoc.CreateElement("flag");
xesub.InnerText = "1";
xe.AppendChild(xesub);
}
xmlDoc.Save("UserInfo1.xml");
}
}
class LinqXML
{
public static void CreateFunctionalXmlElement()
{
// A "functional" approach to build an
// XML element in memory.
XElement inventory =
new XElement("Inventory",
new XElement("Car", new XAttribute("ID", "1"),
new XElement("Color", "Green"),
new XElement("Make", "BMW"),
new XElement("PetName", "Stan")
)
);
// Call ToString() on our XElement.
Console.WriteLine(inventory);
}
public static void CreateFunctionalXmlDoc()
{
XDocument inventoryDoc =
new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XComment("Current Inventory of AutoLot"),
new XElement("Inventory",
new XElement("Car", new XAttribute("ID", "1"),
new XElement("Color", "Green"),
new XElement("Make", "BMW"),
new XElement("PetName", "Stan")
),
new XElement("Car", new XAttribute("ID", "2"),
new XElement("Color", "Pink"),
new XElement("Make", "Yugo"),
new XElement("PetName", "Melvin")
)
)
);
// Display the document and save to disk.
Console.WriteLine(inventoryDoc);
inventoryDoc.Save("SimpleInventory.xml");
}
public static void CreateXmlDocFromArray()
{
// Create an anonymous array of anonymous types.
var data = new[] {
new { PetName = "Melvin", ID = 10 },
new { PetName = "Pat", ID = 11 },
new { PetName = "Danny", ID = 12 },
new { PetName = "Clunker", ID = 13 }
};
// Now enumerate over the array to build
// an XElement.
XElement vehicles =
new XElement("Inventory",
from c in data
select new XElement("Car",
new XAttribute("ID", c.ID),
new XElement("PetName", c.PetName)
)
);
Console.WriteLine(vehicles);
}
public static void LoadExistingXml()
{
// Build an XElement from string.
string myElement =
@"<Car ID ='3'>
<Color>Yellow</Color>
<Make>Yugo</Make>
</Car>";
XElement newElement = XElement.Parse(myElement);
Console.WriteLine(newElement);
Console.WriteLine();
// Load the SimpleInventory.xml file.
XDocument myDoc = XDocument.Load("SimpleInventory.xml");
Console.WriteLine(myDoc);
}
public static void PrintAllPetNames(XElement doc)
{
var petNames = from pn in doc.Descendants("PetName")
select pn.Value;
foreach (var name in petNames)
Console.WriteLine("Name: {0}", name);
}
public static void GetAllFords(XElement doc)
{
var fords = from c in doc.Descendants("Make")
where c.Value == "Ford"
select c;
foreach (var f in fords)
Console.WriteLine("Name: {0}", f);
}
public static void AddNewElements(XElement doc)
{
// Add 5 new purple Fords to the incoming document.
for (int i = 0; i < 5; i++)
{
// Create a new XElement
XElement newCar =
new XElement("Car", new XAttribute("ID", i + 1000),
new XElement("Color", "Green"),
new XElement("Make", "Ford"),
new XElement("PetName", "")
);
// Add to doc.
doc.Add(newCar);
}
// Show the updates.
Console.WriteLine(doc);
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication2
{
class OperateXML
{
XmlDocument xmldoc;
XmlNode xmlnode;
XmlElement xmlelem;
public void CreateDocByDoc()
{
xmldoc = new XmlDocument();
XmlDeclaration xDecl = xmldoc.CreateXmlDeclaration("1.0", "Unicode", null);
xmldoc.AppendChild(xDecl);
xmlelem = xmldoc.CreateElement("Employees");
xmldoc.AppendChild(xmlelem);
for (int i = 0; i < 3; i++)
{
XmlNode root = xmldoc.SelectSingleNode("Employees");
XmlElement xe1 = xmldoc.CreateElement("Node");
xe1.SetAttribute("genre", "李赞红");
xe1.SetAttribute("ISBN", "2-3631-4");
XmlElement xesub1 = xmldoc.CreateElement("title");
xesub1.InnerText = "CS从入门到精通";
xe1.AppendChild(xesub1);
XmlElement xesub2 = xmldoc.CreateElement("author");
xesub2.InnerText = "候捷";
xe1.AppendChild(xesub2);
XmlElement xesub3 = xmldoc.CreateElement("price");
xesub3.InnerText = "58.3";
xe1.AppendChild(xesub3);
root.AppendChild(xe1);
}
xmldoc.Save("UserInfo1.xml");
}
public void CreateDocByXMLWriter()
{
XmlTextWriter xmlWriter;
string strFilename = "UserInfo2.xml";
xmlWriter = new XmlTextWriter(strFilename, Encoding.Unicode);//创建一个xml文档
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("Employees");
xmlWriter.WriteStartElement("Node");
xmlWriter.WriteAttributeString("genre", "李赞红");
xmlWriter.WriteAttributeString("ISBN", "2-3631-4");
xmlWriter.WriteStartElement("title");
xmlWriter.WriteString("CS从入门到精通");
xmlWriter.WriteEndElement();
xmlWriter.WriteStartElement("author");
xmlWriter.WriteString("候捷");
xmlWriter.WriteEndElement();
xmlWriter.WriteStartElement("price");
xmlWriter.WriteString("58.3");
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
xmlWriter.Close();
}
public void AddNode()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("UserInfo1.xml");
XmlNode root = xmlDoc.SelectSingleNode("Employees");//查找<Employees>
XmlElement xe1 = xmlDoc.CreateElement("Node");//创建一个<Node>节点
xe1.SetAttribute("genre", "张三");//设置该节点genre属性
xe1.SetAttribute("ISBN", "1-1111-1");//设置该节点ISBN属性
XmlElement xesub1 = xmlDoc.CreateElement("title");
xesub1.InnerText = "C#入门帮助";//设置文本节点
xe1.AppendChild(xesub1);//添加到<Node>节点中
XmlElement xesub2 = xmlDoc.CreateElement("author");
xesub2.InnerText = "高手";
xe1.AppendChild(xesub2);
XmlElement xesub3 = xmlDoc.CreateElement("price");
xesub3.InnerText = "158.3";
xe1.AppendChild(xesub3);
root.AppendChild(xe1);//添加到<Employees>节点中
xmlDoc.Save("UserInfo1.xml");
}
public void EditDocData()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("UserInfo1.xml");
XmlNodeList nodeList = xmlDoc.SelectSingleNode("Employees").ChildNodes;//获取Employees节点的所有子节点
foreach (XmlNode xn in nodeList)//遍历所有子节点
{
XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型
if (xe.GetAttribute("genre") == "张三")//如果genre属性值为“张三”
{
xe.SetAttribute("genre", "update张三");//则修改该属性为“update张三”
XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点
foreach (XmlNode xn1 in nls)//遍历
{
XmlElement xe2 = (XmlElement)xn1;//转换类型
if (xe2.Name == "author")//如果找到
{
xe2.InnerText = "亚胜";//则修改
}
}
}
}
xmlDoc.Save("UserInfo1.xml");//保存。
}
public void EditDocStructure()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("UserInfo1.xml");
XmlNodeList nodeList = xmlDoc.SelectSingleNode("Employees").ChildNodes;//获取Employees节点的所有子节点
foreach (XmlNode xn in nodeList)
{
XmlElement xe = (XmlElement)xn;
xe.SetAttribute("test", "111111");
XmlElement xesub = xmlDoc.CreateElement("flag");
xesub.InnerText = "1";
xe.AppendChild(xesub);
}
xmlDoc.Save("UserInfo1.xml");
}
}
class LinqXML
{
public static void CreateFunctionalXmlElement()
{
// A "functional" approach to build an
// XML element in memory.
XElement inventory =
new XElement("Inventory",
new XElement("Car", new XAttribute("ID", "1"),
new XElement("Color", "Green"),
new XElement("Make", "BMW"),
new XElement("PetName", "Stan")
)
);
// Call ToString() on our XElement.
Console.WriteLine(inventory);
}
public static void CreateFunctionalXmlDoc()
{
XDocument inventoryDoc =
new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XComment("Current Inventory of AutoLot"),
new XElement("Inventory",
new XElement("Car", new XAttribute("ID", "1"),
new XElement("Color", "Green"),
new XElement("Make", "BMW"),
new XElement("PetName", "Stan")
),
new XElement("Car", new XAttribute("ID", "2"),
new XElement("Color", "Pink"),
new XElement("Make", "Yugo"),
new XElement("PetName", "Melvin")
)
)
);
// Display the document and save to disk.
Console.WriteLine(inventoryDoc);
inventoryDoc.Save("SimpleInventory.xml");
}
public static void CreateXmlDocFromArray()
{
// Create an anonymous array of anonymous types.
var data = new[] {
new { PetName = "Melvin", ID = 10 },
new { PetName = "Pat", ID = 11 },
new { PetName = "Danny", ID = 12 },
new { PetName = "Clunker", ID = 13 }
};
// Now enumerate over the array to build
// an XElement.
XElement vehicles =
new XElement("Inventory",
from c in data
select new XElement("Car",
new XAttribute("ID", c.ID),
new XElement("PetName", c.PetName)
)
);
Console.WriteLine(vehicles);
}
public static void LoadExistingXml()
{
// Build an XElement from string.
string myElement =
@"<Car ID ='3'>
<Color>Yellow</Color>
<Make>Yugo</Make>
</Car>";
XElement newElement = XElement.Parse(myElement);
Console.WriteLine(newElement);
Console.WriteLine();
// Load the SimpleInventory.xml file.
XDocument myDoc = XDocument.Load("SimpleInventory.xml");
Console.WriteLine(myDoc);
}
public static void PrintAllPetNames(XElement doc)
{
var petNames = from pn in doc.Descendants("PetName")
select pn.Value;
foreach (var name in petNames)
Console.WriteLine("Name: {0}", name);
}
public static void GetAllFords(XElement doc)
{
var fords = from c in doc.Descendants("Make")
where c.Value == "Ford"
select c;
foreach (var f in fords)
Console.WriteLine("Name: {0}", f);
}
public static void AddNewElements(XElement doc)
{
// Add 5 new purple Fords to the incoming document.
for (int i = 0; i < 5; i++)
{
// Create a new XElement
XElement newCar =
new XElement("Car", new XAttribute("ID", i + 1000),
new XElement("Color", "Green"),
new XElement("Make", "Ford"),
new XElement("PetName", "")
);
// Add to doc.
doc.Add(newCar);
}
// Show the updates.
Console.WriteLine(doc);
}
}
}