专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > DotNet Exception

缺少根元素

发布时间:2011-06-26 20:25:21 文章来源:www.iduyao.cn 采编人员:星星草

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

友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: