C # 创建简单的 xml 文件

如何创建一个简单的 xml 文件并将其存储在系统中?

207548 次浏览

你可以使用 XDocument:

new XDocument(
new XElement("root",
new XElement("someNode", "someValue")
)
)
.Save("foo.xml");

如果要创建的文件非常大,无法放入内存,则可以使用 XmlWriter

我建议序列化,

public class Person
{
public  string FirstName;
public  string MI;
public  string LastName;
}


static void Serialize()
{
clsPerson p = new Person();
p.FirstName = "Jeff";
p.MI = "A";
p.LastName = "Price";
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(p.GetType());
x.Serialize(System.Console.Out, p);
System.Console.WriteLine();
System.Console.WriteLine(" --- Press any key to continue --- ");
System.Console.ReadKey();
}

可以进一步使用属性控制序列化。
但是如果它很简单,您可以使用 XmlDocument:

using System;
using System.Xml;


public class GenerateXml {
private static void Main() {
XmlDocument doc = new XmlDocument();
XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(docNode);


XmlNode productsNode = doc.CreateElement("products");
doc.AppendChild(productsNode);


XmlNode productNode = doc.CreateElement("product");
XmlAttribute productAttribute = doc.CreateAttribute("id");
productAttribute.Value = "01";
productNode.Attributes.Append(productAttribute);
productsNode.AppendChild(productNode);


XmlNode nameNode = doc.CreateElement("Name");
nameNode.AppendChild(doc.CreateTextNode("Java"));
productNode.AppendChild(nameNode);
XmlNode priceNode = doc.CreateElement("Price");
priceNode.AppendChild(doc.CreateTextNode("Free"));
productNode.AppendChild(priceNode);


// Create and add another product node.
productNode = doc.CreateElement("product");
productAttribute = doc.CreateAttribute("id");
productAttribute.Value = "02";
productNode.Attributes.Append(productAttribute);
productsNode.AppendChild(productNode);
nameNode = doc.CreateElement("Name");
nameNode.AppendChild(doc.CreateTextNode("C#"));
productNode.AppendChild(nameNode);
priceNode = doc.CreateElement("Price");
priceNode.AppendChild(doc.CreateTextNode("Free"));
productNode.AppendChild(priceNode);


doc.Save(Console.Out);
}
}

如果需要快速,可以使用 XmlWriter:

public static void WriteXML()
{
// Create an XmlWriterSettings object with the correct options.
System.Xml.XmlWriterSettings settings = new System.Xml.XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = "    "; //  "\t";
settings.OmitXmlDeclaration = false;
settings.Encoding = System.Text.Encoding.UTF8;


using (System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create("data.xml", settings))
{


writer.WriteStartDocument();
writer.WriteStartElement("books");


for (int i = 0; i < 100; ++i)
{
writer.WriteStartElement("book");
writer.WriteElementString("item", "Book "+ (i+1).ToString());
writer.WriteEndElement();
}


writer.WriteEndElement();


writer.Flush();
writer.Close();
} // End Using writer


}

顺便说一句,读取 XML 的最快方法是 XmlReader:

public static void ReadXML()
{
using (System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml"))
{
while (xmlReader.Read())
{
if ((xmlReader.NodeType == System.Xml.XmlNodeType.Element) && (xmlReader.Name == "Cube"))
{
if (xmlReader.HasAttributes)
System.Console.WriteLine(xmlReader.GetAttribute("currency") + ": " + xmlReader.GetAttribute("rate"));
}


} // Whend


} // End Using xmlReader


System.Console.ReadKey();
}

读取 XML 最方便的方法是将 XML 反序列化为一个类。
顺便说一下,这对于创建序列化类也有效。
您可以使用 Xml2CSharp从 XML 生成类:
Https://xmltocsharp.azurewebsites.net/