c# 生成 xml 文件 (转)
2009-09-04 11:57
447 查看
c# 生成 xml 文件
方法一:
using System;
using System.Xml;
using System.IO;
using System.Text;
public class ReadWriteXml
{
private static void Main()
{
// Create the file and writer.
FileStream fs = new FileStream("products.xml", FileMode.Create);
XmlTextWriter w = new XmlTextWriter(fs, Encoding.UTF8);
// Start the document.
w.WriteStartDocument();
w.WriteStartElement("products");
// Write a product.
w.WriteStartElement("product");
w.WriteAttributeString("id", "1001");
w.WriteElementString("productName", "Gourmet Coffee");
w.WriteElementString("productPrice", "0.99");
w.WriteEndElement();
// Write another product.
w.WriteStartElement("product");
w.WriteAttributeString("id", "1002");
w.WriteElementString("productName", "Blue China Tea Pot");
w.WriteElementString("productPrice", "102.99");
w.WriteEndElement();
// End the document.
w.WriteEndElement();
w.WriteEndDocument();
w.Flush();
fs.Close();
Console.WriteLine("Document created. " +
"Press Enter to read the document.");
Console.ReadLine();
fs = new FileStream("products.xml", FileMode.Open);
XmlTextReader r = new XmlTextReader(fs);
// Read all nodes.
while (r.Read())
{
if (r.NodeType == XmlNodeType.Element)
{
Console.WriteLine();
Console.WriteLine("<" + r.Name + ">");
if (r.HasAttributes)
{
for (int i = 0; i < r.AttributeCount; i++)
{
Console.WriteLine("\tATTRIBUTE: " +
r.GetAttribute(i));
}
}
}
else if (r.NodeType == XmlNodeType.Text)
{
Console.WriteLine("\tVALUE: " + r.Value);
}
}
Console.ReadLine();
}
}
方法二:
1using System;
2using System.Xml;
3
4public class GenerateXml
5{
6
7 private static void Main()
8 {
9
10 // Create a new, empty document.
11 XmlDocument doc = new XmlDocument();
12 XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
13 doc.AppendChild(docNode);
14
15 // Create and insert a new element.
16 XmlNode productsNode = doc.CreateElement("products");
17 doc.AppendChild(productsNode);
18
19 // Create a nested element (with an attribute).
20 XmlNode productNode = doc.CreateElement("product");
21 XmlAttribute productAttribute = doc.CreateAttribute("id");
22 productAttribute.Value = "1001";
23 productNode.Attributes.Append(productAttribute);
24 productsNode.AppendChild(productNode);
25
26 // Create and add the sub-elements for this product node
27 // (with contained text data).
28 XmlNode nameNode = doc.CreateElement("productName");
29 nameNode.AppendChild(doc.CreateTextNode("Gourmet Coffee"));
30 productNode.AppendChild(nameNode);
31 XmlNode priceNode = doc.CreateElement("productPrice");
32 priceNode.AppendChild(doc.CreateTextNode("0.99"));
33 productNode.AppendChild(priceNode);
34
35 // Create and add another product node.
36 productNode = doc.CreateElement("product");
37 productAttribute = doc.CreateAttribute("id");
38 productAttribute.Value = "1002";
39 productNode.Attributes.Append(productAttribute);
40 productsNode.AppendChild(productNode);
41 nameNode = doc.CreateElement("productName");
42 nameNode.AppendChild(doc.CreateTextNode("Blue China Tea Pot"));
43 productNode.AppendChild(nameNode);
44 priceNode = doc.CreateElement("productPrice");
45 priceNode.AppendChild(doc.CreateTextNode("102.99"));
46 productNode.AppendChild(priceNode);
47
48 // Save the document (to the Console window rather than a file).
49 doc.Save(Console.Out);
50 Console.ReadLine();
51 }
52}
53
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/jupiter97/archive/2007/05/15/1610176.aspx
方法一:
using System;
using System.Xml;
using System.IO;
using System.Text;
public class ReadWriteXml
{
private static void Main()
{
// Create the file and writer.
FileStream fs = new FileStream("products.xml", FileMode.Create);
XmlTextWriter w = new XmlTextWriter(fs, Encoding.UTF8);
// Start the document.
w.WriteStartDocument();
w.WriteStartElement("products");
// Write a product.
w.WriteStartElement("product");
w.WriteAttributeString("id", "1001");
w.WriteElementString("productName", "Gourmet Coffee");
w.WriteElementString("productPrice", "0.99");
w.WriteEndElement();
// Write another product.
w.WriteStartElement("product");
w.WriteAttributeString("id", "1002");
w.WriteElementString("productName", "Blue China Tea Pot");
w.WriteElementString("productPrice", "102.99");
w.WriteEndElement();
// End the document.
w.WriteEndElement();
w.WriteEndDocument();
w.Flush();
fs.Close();
Console.WriteLine("Document created. " +
"Press Enter to read the document.");
Console.ReadLine();
fs = new FileStream("products.xml", FileMode.Open);
XmlTextReader r = new XmlTextReader(fs);
// Read all nodes.
while (r.Read())
{
if (r.NodeType == XmlNodeType.Element)
{
Console.WriteLine();
Console.WriteLine("<" + r.Name + ">");
if (r.HasAttributes)
{
for (int i = 0; i < r.AttributeCount; i++)
{
Console.WriteLine("\tATTRIBUTE: " +
r.GetAttribute(i));
}
}
}
else if (r.NodeType == XmlNodeType.Text)
{
Console.WriteLine("\tVALUE: " + r.Value);
}
}
Console.ReadLine();
}
}
方法二:
1using System;
2using System.Xml;
3
4public class GenerateXml
5{
6
7 private static void Main()
8 {
9
10 // Create a new, empty document.
11 XmlDocument doc = new XmlDocument();
12 XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
13 doc.AppendChild(docNode);
14
15 // Create and insert a new element.
16 XmlNode productsNode = doc.CreateElement("products");
17 doc.AppendChild(productsNode);
18
19 // Create a nested element (with an attribute).
20 XmlNode productNode = doc.CreateElement("product");
21 XmlAttribute productAttribute = doc.CreateAttribute("id");
22 productAttribute.Value = "1001";
23 productNode.Attributes.Append(productAttribute);
24 productsNode.AppendChild(productNode);
25
26 // Create and add the sub-elements for this product node
27 // (with contained text data).
28 XmlNode nameNode = doc.CreateElement("productName");
29 nameNode.AppendChild(doc.CreateTextNode("Gourmet Coffee"));
30 productNode.AppendChild(nameNode);
31 XmlNode priceNode = doc.CreateElement("productPrice");
32 priceNode.AppendChild(doc.CreateTextNode("0.99"));
33 productNode.AppendChild(priceNode);
34
35 // Create and add another product node.
36 productNode = doc.CreateElement("product");
37 productAttribute = doc.CreateAttribute("id");
38 productAttribute.Value = "1002";
39 productNode.Attributes.Append(productAttribute);
40 productsNode.AppendChild(productNode);
41 nameNode = doc.CreateElement("productName");
42 nameNode.AppendChild(doc.CreateTextNode("Blue China Tea Pot"));
43 productNode.AppendChild(nameNode);
44 priceNode = doc.CreateElement("productPrice");
45 priceNode.AppendChild(doc.CreateTextNode("102.99"));
46 productNode.AppendChild(priceNode);
47
48 // Save the document (to the Console window rather than a file).
49 doc.Save(Console.Out);
50 Console.ReadLine();
51 }
52}
53
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/jupiter97/archive/2007/05/15/1610176.aspx
相关文章推荐
- C#代码生成XML文件
- C# 利用自带xsd.exe工具操作XML-如通过XML生成xsd文件
- c# 生成 xml 文件
- C# 生成库表xml 文件
- C# 生成xml文件
- C# 选择文件夹,读取文件夹下文件和文件夹生成XML
- c#动态生成xml文件
- C#中XML文档生成实例:Elevator Scheduler测试文件的生成详解
- [C#]包含DataSet类的XML架构怎么不自动生成CS文件了(XSD和CS文件不能合并)
- asp.net(C#)从数据库中获取 BLOB 值,生成xml文件
- C#中读取xml文件和生成xml文件
- C# 利用自带xsd.exe工具操作XML-如通过XML生成xsd文件
- c#:Dataset读取XML文件动态生成菜单
- c#生成XML字符串,中间过程不生成本地文件,返回字符串。
- C# 利用自带xsd.exe工具操作XML-如通过XML生成xsd文件
- c# 生成 xml 文件
- C#如何生成一个XML文件,并保存在硬盘的指定目录下
- c#使用Dataset读取XML文件动态生成菜单的方法
- c# 使用数据生成xml文件
- 通过xml模板生成意见审批表word文件