首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

缺少根元素

2014-01-28 
读取xml报缺少根元素using System using System.IO using System.Xml namespace HelloXml { class XmlEx

读取xml报缺少根元素

using System;
using System.IO;
using System.Xml;

namespace HelloXml
{
class XmlEx
{
public static void Main()
{
//
//Create temp xml stream/file in the memory
//
MemoryStream mem = new MemoryStream();

XmlTextWriter xtw = new XmlTextWriter
(
mem,
null
);

xtw.Formatting = System.Xml.Formatting.Indented;

try
{
xtw.WriteStartDocument( false );
xtw.WriteElementString( "Hello", "aaa" );
xtw.WriteEndDocument();
}
catch( Exception ex_1 )
{
Console.WriteLine( "While creating memory xml operation error: {0}", ex_1.Message );
return;
}

try
{
//
//Begin to create the real xml file
//
XmlDocument doc = new XmlDocument();
try
{
doc.Load( mem );
}
catch( Exception ex_2 )
{
Console.WriteLine( "Load memory xml error: {0}", ex_2.Message );
return;
}

XmlNode rootNode = doc.CreateElement( "Hello" );
rootNode.InnerText = "HelloWorld";

doc.DocumentElement.AppendChild( rootNode );

xtw.Close();
xtw = new XmlTextWriter
(
"c:\\HelloEdit.xml",
System.Text.Encoding.ASCII
);         

热点排行