English 中文(简体)
失踪的根源——使用XmlTextWriter创建Xmldocument
原标题:Root Element Missing - Creating Xmldocument using XmlTextWriter
  • 时间:2010-07-14 14:45:14
  •  标签:
  • c#
  • .net
  • xml

I m有以下代码,在doc.Load(期间丢失了。

MemoryStream stream = new MemoryStream();
XmlTextWriter xmlWriter = new XmlTextWriter(stream, Encoding.UTF8);
xmlWriter.Formatting = System.Xml.Formatting.Indented;
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("Root");
XmlDocument doc = new XmlDocument();
stream.Position = 0;
doc.Load(stream);
xmlWriter.Close();

我无法指出这一问题。 任何见解?

最佳回答

页: 1 此外,你也从不完成根本内容,即使其has写成

<Root>

它胜诉,撰写了结对。 页: 1

我不敢肯定,在什么时候,XmlWriter 实际上是写明任何内容的起始部分——也不要忘记它可能具有撰写特征。 最能用您的密码书写的是<Root

这里有一个完整的方案:

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

class Test
{
    static void Main(string[] args)
    {
        using (MemoryStream stream = new MemoryStream())
        {
            XmlTextWriter xmlWriter = new XmlTextWriter(stream, Encoding.UTF8);
            xmlWriter.Formatting = System.Xml.Formatting.Indented;
            xmlWriter.WriteStartDocument();
            xmlWriter.WriteStartElement("Root");
            xmlWriter.WriteEndElement();
            xmlWriter.Flush();

            XmlDocument doc = new XmlDocument();
            stream.Position = 0;
            doc.Load(stream);
            doc.Save(Console.Out);
        }
    }
}

(请注意:我不打上<条码>。

问题回答

@Skeet说,加上你,你似乎没有结束你的内容:

xmlWriter.WriteEndElement();

我看见你开始该文件,但我看不见:http://msdn.microsoft.com/en-us/library/system.xml.xmltext author.wenddocument.aspx” rel=“nofollow noreferer”> ending。 或.closing. (至少待您使用)





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签