方法一:

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. LeetCode(88)Merge Sorted Array

    题目 Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note ...

  2. 百度地图离线API 2.0(含示例,可完全断网访问)

    由于公司需求,自己修改的离线地图API.该压缩包具有如下功能:1.支持使用google地图瓦片(不建议使用,效率不高,缩放级别较高时拖动有些卡顿,建议注释该代码块:overlayTileLayer.g ...

  3. 图解使用IDEA创建第一个Java程序HelloWorld

    前几次给大家分享了怎么在自己的电脑上配置 java 环境,准备工作做好了,我们就要开始我们真正的编码学习了.下面介绍使用 IDEA 创建我们的第一个 HelloWorld 程序. 1.打开 IDEA, ...

  4. STM32F407 跑马灯 寄存器版 个人笔记

    更多原理请参考跑马灯 库函数版 个人笔记 步骤 使能IO口时钟.配置相关寄存器寄存器RCC->AHB1ENR 初始化IO口模式.配置四个配置寄存器 GPIOx_MODER/ GPIOx_OTYP ...

  5. Android渲染器Shader:LinearGradient(一)

     Android渲染器Shader:LinearGradient(一) LinearGradient是Android的线性渲染器.我写5个LinearGradient渲染器渲染后的View表现结果 ...

  6. android源码mm时的编译错误no ruler to make target `out/target/common/obj/JAVA_LIBRARIES/xxxx/javalib.jar', needed by `out/target/common/obj/APPS/xxxx_intermediates/classes-full-debug.jar'. Stop.

    瞧见没有,就因为多了这一个反斜杠,浪费了一下午时间找问题,哭了~~~~

  7. java web 验证码 第一次不正确的问题,解决方案

    首先是form表单 ,获取图片验证码 然后使用js 去服务器验证 问题: 第一次明明输入正确 ,确验证不了??那是因为你在form表单发起请求 和 ajax  发起的请求  地址 中 一个使用127. ...

  8. 洛谷 P3456 [POI2007]GRZ-Ridges and Valleys

    P3456 [POI2007]GRZ-Ridges and Valleys 题意翻译 给定一个地图,为小朋友想要旅行的区域,地图被分为n*n的网格,每个格子(i,j) 的高度w(i,j)是给定的.若两 ...

  9. JDBC示例(增删查改)

    前提: 1.项目中引入MySQL的JAR包,POM参考如下配置: <!-- mysql-connector-java --> <!-- http://mvnrepository.co ...

  10. SetWindowsHookEx详解

    http://blog.csdn.net/mmllkkjj/article/details/6627188 函数功能:该函数将一个应用程序定义的挂钩处理过程安装到挂钩链中去,您可以通过安装挂钩处理过程 ...