读取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
);