方法一:

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", "");
w.WriteElementString("productName", "Gourmet Coffee");
w.WriteElementString("productPrice", "0.99");
w.WriteEndElement(); // Write another product.
w.WriteStartElement("product");
w.WriteAttributeString("id", "");
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 = ; 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;
public class GenerateXml
{ private static void Main()
{ // Create a new, empty document.
XmlDocument doc = new XmlDocument();
XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(docNode); // Create and insert a new element.
XmlNode productsNode = doc.CreateElement("products");
doc.AppendChild(productsNode); // Create a nested element (with an attribute).
XmlNode productNode = doc.CreateElement("product");
XmlAttribute productAttribute = doc.CreateAttribute("id");
productAttribute.Value = "";
productNode.Attributes.Append(productAttribute);
productsNode.AppendChild(productNode); // Create and add the sub-elements for this product node
// (with contained text data).
XmlNode nameNode = doc.CreateElement("productName");
nameNode.AppendChild(doc.CreateTextNode("Gourmet Coffee"));
productNode.AppendChild(nameNode);
XmlNode priceNode = doc.CreateElement("productPrice");
priceNode.AppendChild(doc.CreateTextNode("0.99"));
productNode.AppendChild(priceNode); // Create and add another product node.
productNode = doc.CreateElement("product");
productAttribute = doc.CreateAttribute("id");
productAttribute.Value = "";
productNode.Attributes.Append(productAttribute);
productsNode.AppendChild(productNode);
nameNode = doc.CreateElement("productName");
nameNode.AppendChild(doc.CreateTextNode("Blue China Tea Pot"));
productNode.AppendChild(nameNode);
priceNode = doc.CreateElement("productPrice");
priceNode.AppendChild(doc.CreateTextNode("102.99"));
productNode.AppendChild(priceNode); // Save the document (to the Console window rather than a file).
doc.Save(Console.Out);
Console.ReadLine();
}
}

c# 生成 xml 文件的更多相关文章

  1. Android 解析XML文件和生成XML文件

    解析XML文件 public static void initXML(Context context) { //can't create in /data/media/0 because permis ...

  2. Java生成XML文件

    我们在数据库中的数据可以将其提取出来生成XML文件,方便传输.例如数据库中有Admin这张表: 我们写一个java类表示admin数据: package xmlDom.vo; import java. ...

  3. Android 使用xml序列化器生成xml文件

    在<Android 生成xml文件>一文中使用流的形式写入xml格式文件,但是存在一定的问题,那就是在短信内容中不能出现<>之类的括号,本文使用xml序列化器来解决 xml序列 ...

  4. C# 生成xml文件

    本篇文章旨在.net环境下生成xml文件,以控制台应用程序为例进行说明. 1.在vs中新建控制台应用程序CreateXml 2.CreateXmlFile:主要生成xml的函数 public void ...

  5. 视频播放实时记录日志并生成XML文件

    需求描述: 在JWPlayer视频播放过程中,要求实时记录视频观看者播放.暂停的时间,并记录从暂停到下一次播放时所经过的时间.将所有记录保存为XML文件,以方便数据库的后续使用. 实现过程: 尝试1: ...

  6. 使用XML序列化器生成XML文件和利用pull解析XML文件

    首先,指定XML格式,我指定的XML格式如下: <?xml version='1.0' encoding='utf-8' standalone='yes' ?> <message&g ...

  7. LINQ to XML 从逗号分隔值 (CSV) 文件生成 XML 文件

    参考:http://msdn.microsoft.com/zh-cn/library/bb387090.aspx 本示例演示如何使用 语言集成查询 (LINQ) 和 LINQ to XML 从逗号分隔 ...

  8. 生成XML文件,通过实体生成XML文件

    实体 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xm ...

  9. PHP中的生成XML文件的4种方法(转)

    <?xml version="1.0" encoding="utf-8"?> <article> <item> <ti ...

  10. 使用XMl序列化器生成xml文件

    生成XML文件 创建几个虚拟的短信对象,存在list中 备份数据通常都是备份至sd卡 使用StringBuffer拼接字符串 把整个xml文件所有节点append到sb对象里 sb.append(&q ...

随机推荐

  1. 牛客网补题 New Game!(原Wannafly summer camp day2原题)

    思路:这个题在秦皇岛的时候好像没有写出来,反正我是没有写出来,题解是听懂了:把直线和圆都看做一个结点,圆和直线用点到直线的距离与半径差求出来,圆和圆之间用点和点之间的距离和半径差表示,最后最短路跑一遍 ...

  2. Windows和Linux启动虚拟环境

    快速跳转到Linux操作 Windows启动虚拟环境 <!--tab回车可以补全--> 安装virtualenv pip install virtualenv 创建虚拟环境 方法一: py ...

  3. Codeforces Round #364 (Div. 2),只有A与B

    A. Cards time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  4. [luoguP1045] 麦森数(快速幂 + 高精度)

    传送门 这道题纯粹是考数学.编程复杂度不大(别看我写了一百多行其实有些是可以不必写的). 计算位数不必用高精时刻存,不然可想而知时间复杂度之大.首先大家要知道一个数学公式 logn(a*b)=logn ...

  5. 舒适的路线(codevs 1001)

    题目描述 Description Z小镇是一个景色宜人的地方,吸引来自各地的观光客来此旅游观光.Z小镇附近共有N(1<N≤500)个景点(编号为1,2,3,…,N),这些景点被M(0<M≤ ...

  6. Quartz.net框架使用

    概述:Quartz.NET是一个开源的作业调度框架,非常适合在平时的工作中,定时轮询数据库同步,定时邮件通知,定时处理数据等. Quartz.NET允许开发人员根据时间间隔(或天)来调度作业.它实现了 ...

  7. 【BZOJ2330】糖果(差分约束系统,强连通分量,拓扑排序)

    题意: 幼儿园里有N个小朋友,lxhgww老师现在想要给这些小朋友们分配糖果,要求每个小朋友都要分到糖果.但是小朋友们也有嫉妒心,总是会提出一些要求,比如小明不希望小红分到的糖果比他的多,于是在分配糖 ...

  8. codevs 1378 选课

    题目描述 Description 学校实行学分制.每门的必修课都有固定的学分,同时还必须获得相应的选修课程学分.学校开设了N(N<300)门的选修课程,每个学生可选课程的数量M是给定的.学生选修 ...

  9. [转] 结构体file_operations

    原文地址: http://www.cnblogs.com/sunyubo/archive/2010/12/22/2282079.html 结构体file_operations在头文件 linux/fs ...

  10. Num 15: NYOJ: 题目0002 : 括号配对问题 [ 栈(stack) ]

    原题连接      首先要了解有关栈的一些基本知识,即:      什么是栈,栈有什么作用:        1.什么是栈: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkb ...