开发者

C# XML can't load indent value

开发者 https://www.devze.com 2023-03-11 04:55 出处:网络
The code below is causing the \" Data at the root level is invalid. Line 1, position 1. \" I like to keep the code indent(linebreak) but keep having the problem as mentioned above. I could use the Te

The code below is causing the " Data at the root level is invalid. Line 1, position 1. " I like to keep the code indent(linebreak) but keep having the problem as mentioned above. I could use the TextReader to load the xml but it will remove the indent which i don't like it. If you know how to fix problem please let me know. Thanks

public XmlDocument MYXML()
  {
    XmlWriterSettings wSettings = new XmlWriterSettings();
      wSettings.Indent = false;
      wSettings.OmitXmlDeclaration = false;
      MemoryStream ms = new MemoryStream();
      XmlWriter xw = XmlWriter.Create(ms, wSettings);// Write Declaration
      xw.WriteStartDocument();

      // W开发者_Go百科rite the root node
      xw.WriteStartElement("Library");

      // Write the books and the book elements
      xw.WriteStartElement("Book");
      xw.WriteStartAttribute("BookType");
      xw.WriteString("Hardback");
      xw.WriteEndAttribute();

      xw.WriteStartElement("Title");
      xw.WriteString("Door Number Three");
      xw.WriteEndElement();
      xw.WriteStartElement("Author");
      xw.WriteString("O'Leary, Patrick");
      xw.WriteEndElement();

      xw.WriteEndElement();

      xw.WriteEndElement();

      // Close the document
      xw.WriteEndDocument();

      // Flush the write
      xw.Flush();

      Byte[] buffer = new Byte[ms.Length];
      buffer = ms.ToArray();
      string xmlOutput = System.Text.Encoding.UTF8.GetString(buffer);

      //The next 3 line works fine but it will remove the Indent from the XmlWriterSettings
      //TextReader tr = new StreamReader(ms);
      //ms.Seek(0, SeekOrigin.Begin);
      //xmlOutput = tr.ReadToEnd() + "";

      //Can't nload the  xmlOutput from  buffer
      XmlDocument xmldoc = new XmlDocument();
      xmldoc.LoadXml(xmlOutput);

      return xmldoc;
  }


The XmlWriter is writing a utf-8 byte-order mark to the stream. Encoding.UTF8.GetString doesn't account for this (since it's only supposed to occur in files) so the first character of the string becomes an invalid, unprintable character, which is what XmlDocument.LoadXml chokes on.

EDIT: Since you said you want to create an XmlDocument so you can reuse it, I recommend one of the following:

  1. If using .Net 3.5 or newer, use XDocument which is much easier to use (I recommend this).
  2. Create the XmlDocument directly by constructing nodes and adding them to the tree.
  3. Create the XmlDocument directly from the writer by using XPathNavigator (XmlWriter writer = doc.CreateNavigator.AppendChild())

Note that you can't easily add insignificant whitespace to an XmlDocument. Using XDocument and writing it to the output using doc.Save(Response.Output) is by far the easiest option if you want to have nicely formatted output.

0

精彩评论

暂无评论...
验证码 换一张
取 消