csharp操作xml写xml1.java操作xml写xml

import javax.xml.transform.*;

import javax.xml.transform.stream.*;

import javax.xml.transform.dom.*;

import org.w3c.dom.*;

import javax.xml.parsers.*;

import java.io.*;

import java.util.*;

public class WriteXML

{

public static void main(String[] args)

{  

try

{   

DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();   

DocumentBuilder builder=factory.newDocumentBuilder();   

Document document=builder.newDocument();   

document.setXmlVersion("1.0");   

Comment c=document.createComment("由201211070116完成!");   

document.appendChild(c);   

Element XML指南=document.createElement("XML指南");   

document.appendChild(XML指南);   

Element 章节=document.createElement("章节");   

Element 章=document.createElement("章");   

章.appendChild(document.createTextNode("第一章 XML入门简介"));   

章节.appendChild(章);   

Element 节11=document.createElement("节");   

节11.appendChild(document.createTextNode("1.1节 什么是HTML"));   

章节.appendChild(节11);   

Element 节12=document.createElement("节");   

节12.appendChild(document.createTextNode("1.2节 什么是XML"));   

章节.appendChild(节12);   

XML指南.appendChild(章节);   

Element 章节2=document.createElement("章节");   

Element 章2=document.createElement("章");   

章2.appendChild(document.createTextNode("第二章 XML语法"));   

章节2.appendChild(章2);   

Element 节21=document.createElement("节");   

节21.appendChild(document.createTextNode("2.1节  XML元素必须有结束标签"));   

章节2.appendChild(节21);   

Element 节22=document.createElement("节");   

节22.appendChild(document.createTextNode("2.2节 XML元素必须正确地嵌套"));   

章节2.appendChild(节22);    XML指南.appendChild(章节2);      

TransformerFactory transFactory=TransformerFactory.newInstance();   

Transformer transformer=transFactory.newTransformer();   

Properties properties=transformer.getOutputProperties();   

properties.setProperty(OutputKeys.ENCODING,"GB2312" );   

properties.setProperty(OutputKeys.VERSION,"GB2312" );   

transformer.setOutputProperties(properties);   

transformer.setOutputProperty(OutputKeys.INDENT, "yes");   

DOMSource domSource=new DOMSource(document);   

File file=new File("C:\\JavaProg\\Book.xml");   

FileOutputStream out=new FileOutputStream(file);   

StreamResult xmlResult=new StreamResult(out);   

transformer.transform(domSource, xmlResult);   

System.out.println("OK,成功建立了XML文档!");     

}  

catch(Exception e)  

{   

System.out.println(e);  

}

}

}

运行成功后会在c盘C:\JavaProg文件夹下生成相应的xml文件

2.csharp操作xml写xml

vs里新建项目,选择visual C#-->控制台应用程序,输入以下代码

using System; using System.Xml;

class WirteXML

{    

static void Main(string[] args)    

{       

string fileName = "C:\\JavaProg\\book5.xml";        

XmlWriterSettings seting = new XmlWriterSettings();        

seting.Indent = true;        

seting.Encoding = System.Text.Encoding.Default;        

XmlWriter writer = XmlWriter.Create(fileName, seting);       

writer.WriteStartDocument();        

writer.WriteComment("由201211070116完成!");        

writer.WriteStartElement("XML指南");        

writer.WriteStartElement("章节");        

writer.WriteStartElement("章");        

writer.WriteString("第一章 XML入门简介");       

writer.WriteEndElement();        

writer.WriteStartElement("节");        

writer.WriteString("1.1节 什么是HTML");        

writer.WriteEndElement();        

writer.WriteStartElement("节");       

writer.WriteString("1.2节 什么是XML");       

writer.WriteEndElement();        

writer.WriteEndElement();        

writer.WriteStartElement("章节");        

writer.WriteStartElement("章");        

writer.WriteString("第二章 XML语法");        

writer.WriteEndElement();        

writer.WriteStartElement("节");        

writer.WriteString("2.1节 XML元素必须有结束标签");       

writer.WriteEndElement();        

writer.WriteStartElement("节");        

writer.WriteString("2.2节 XML元素必须正确地嵌套");        

writer.WriteEndElement();        

writer.WriteEndElement();       

writer.WriteEndDocument();        

writer.Close();        

Console.WriteLine("OK,成功地建立了XML文档!");        

Console.ReadLine();

    }

}

运行成功后生成-->解决方案(B)然后在c盘C:\JavaProg文件夹下生成相应的xml文件