如何创建XML文件
XML文件的格式如下,请问如何在WINForm程序中创建?<?xml version="1.0" encoding="gb2312"?>
<FilesInformation>
<version>1.0.1818.42821</version>
<description>说明</description>
<FileItem
FileName="name"
FileVersion="sdf"
FileLength="sdf"
FileCreationTime="sd"
/>
</FilesInformation>
在线等,即可结帖 --------------------编程问答-------------------- 参考:http://hi.baidu.com/%CC%D3%C5%DC%B5%C4%BA%FC%C0%EA/blog/item/f68c66a77cbef994d0435853.html --------------------编程问答-------------------- 给你个例子:
using (XmlTextWriter xmlWr = new XmlTextWriter(strSetsPath, Encoding.Unicode))
{
xmlWr.Formatting = Formatting.Indented;
xmlWr.Indentation = 2;
xmlWr.WriteStartDocument();
xmlWr.WriteStartElement("root");
xmlWr.WriteStartElement("item");
xmlWr.WriteStartElement("title");
xmlWr.WriteString("岗位工资");
xmlWr.WriteEndElement();
xmlWr.WriteStartElement("colName");
xmlWr.WriteString("岗位工资");
xmlWr.WriteEndElement();
xmlWr.WriteStartElement("property");
xmlWr.WriteString("发放");
xmlWr.WriteEndElement();
xmlWr.WriteEndElement();
xmlWr.WriteEndElement();
xmlWr.WriteEndDocument();
} --------------------编程问答-------------------- 一个例子:
using System ;using System.Xml ;class MainClass{XmlDocument xmldoc ;XmlNode xmlnode ;XmlElement xmlelem ;XmlElement xmlelem2 ;XmlText xmltext ;static void Main ( string [ ] args ){MainClass app = new MainClass ( ) ;}public MainClass ( ){xmldoc = new XmlDocument ( ) ;//加入XML的声明段落xmlnode = xmldoc.CreateNode ( XmlNodeType.XmlDeclaration , "" , "" ) ;xmldoc.AppendChild ( xmlnode ) ;//加入一个根元素xmlelem = xmldoc.CreateElement ( "" , "ROOT" , "" ) ;xmltext = xmldoc.CreateTextNode ( "Root Text" ) ;xmlelem.AppendChild ( xmltext ) ;xmldoc.AppendChild ( xmlelem ) ;//加入另外一个元素xmlelem2 = xmldoc.CreateElement ("SampleElement" ) ;xmlelem2 = xmldoc.CreateElement ( "" , "SampleElement" , "" ) ;xmltext = xmldoc.CreateTextNode ( "The text of the sample element" ) ;xmlelem2.AppendChild ( xmltext ) ;xmldoc.ChildNodes.Item(1).AppendChild ( xmlelem2 ) ;//保存创建好的XML文档try{xmldoc.Save ( "c:\\data.xml" ) ; }catch ( Exception e ){//显示错误信息Console.WriteLine ( e.Message ) ;}Console.ReadLine ( ) ;}}--------------------编程问答-------------------- 天啊,格式怎么这样啊。
using System;--------------------编程问答--------------------
using System.Xml ;
class MainClass
{
XmlDocument xmldoc ;
XmlNode xmlnode ;
XmlElement xmlelem ;
XmlElement xmlelem2 ;
XmlText xmltext ;
static void Main ( string [ ] args )
{
MainClass app = new MainClass ( ) ;
}
public MainClass ( )
{
xmldoc = new XmlDocument ( ) ;
//加入XML的声明段落
xmlnode = xmldoc.CreateNode ( XmlNodeType.XmlDeclaration , "" , "" ) ;
xmldoc.AppendChild ( xmlnode ) ;
//加入一个根元素
xmlelem = xmldoc.CreateElement ( "" , "ROOT" , "" ) ;
xmltext = xmldoc.CreateTextNode ( "Root Text" ) ;
xmlelem.AppendChild ( xmltext ) ;
xmldoc.AppendChild ( xmlelem ) ;
//加入另外一个元素
xmlelem2 = xmldoc.CreateElement ("SampleElement" ) ;
xmlelem2 = xmldoc.CreateElement ( "" , "SampleElement" , "" ) ;
xmltext = xmldoc.CreateTextNode ( "The text of the sample element" ) ;
xmlelem2.AppendChild ( xmltext ) ;
xmldoc.ChildNodes.Item(1).AppendChild ( xmlelem2 ) ;
//保存创建好的XML文档
try
{
xmldoc.Save ( "c:\\data.xml" ) ;
}
catch ( Exception e )
{
//显示错误信息
Console.WriteLine ( e.Message ) ;
}
Console.ReadLine ( ) ;
}
}
private void button1_Click(object sender, EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
//建立Xml的定义声明
XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
xmlDoc.AppendChild(dec);
//创建根节点
XmlElement root = xmlDoc.CreateElement("FilesInformation");
xmlDoc.AppendChild(root);
XmlElement version = xmlDoc.CreateElement("version");
version.InnerText = "1.0.1818.42821";
root.AppendChild(version);
XmlElement description = xmlDoc.CreateElement("description");
description.InnerText = "说明";
root.AppendChild(description);
XmlElement fileItem = xmlDoc.CreateElement("FileItem");
fileItem.SetAttribute("FileName", "name");
fileItem.SetAttribute("FileVersion", "xx");
fileItem.SetAttribute("FileLength", "xxx");
fileItem.SetAttribute("FileCreationTime", "xxxx");
root.AppendChild(fileItem);
xmlDoc.Save("test.xml");
}
补充:.NET技术 , C#