You must Sign In to post a response.
  • Category: ASP.NET

    How to remove white space before <xml> tag


    Are you looking for information on XML tags? Want to know how to remove white space before tag? Read this thread to learn more about XML tags with examples.



    Hi,
    My code starts as follows:
    Response.Clear()
    Response.ContentType = "text/xml"

    XmlTextWriter xtwFeed = new XmlTextWriter(Context.Response.OutputStream, System.Text.Encoding.UTF8)


    xtwFeed.WriteStartDocument()

    xtwFeed.WriteStartElement("rss")
    xtwFeed.WriteAttributeString("version", "2.0")

    xtwFeed.WriteStartElement("channel")
    xtwFeed.WriteElementString("title", "Detail")

    ..........
    ..........
    ...........
    xtwFeed.WriteEndElement()

    xtwFeed.WriteEndElement()

    xtwFeed.WriteEndDocument()


    xtwFeed.Flush()

    xtwFeed.Close()

    Response.End()



    But when the xml file is generated it doesnt show the details. The page source shows the content properly. I found that there is space in front of the xml tag.
    Now I need to remove the space before <xml> tag.

    if anyone knw the solution please do post.
    Thanks.
  • #720294
    Hi Niles.
    We can use Trim() before we parse it to XmlParse().
    example: XMLParse(Trim(Data))

    Thanks

    Reena

  • #720318
    Hai Niles,
    There are various ways to remove the spaces from the XML document like:
    1. By using XML Schema
    2. Using XML:space Attribute
    3. Use Canonicalization
    4. Using XML Parser.
    There are few advantages and disadvantages using each of the methods but the Canonicalization is good technique to remove the white spaces.
    For the details description for each of the methods, it proc and cons, you can go through the below link:

    http://www.usingxml.com/Basics/XmlSpace

    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #720332
    you need to parse the xml in order to remove the sapce.
    Use XMLDocument class and load your xml it and then save it.
    it will remove Unnecessary space and enters.
    see the below code snippet


    XMLDocument objDOM = new XMLDocument();
    objDOM.Load("XML file path");
    objDOM.Save();


    hope it helps

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #720335
    Thanks Pawan for your reply..but how to use this in my code...i tried several methods...but it is not working..

    there is a space before this line

    <?xml version="1.0" encoding="utf-8"?>

  • #720413
    Hello Niles,

    Hope this supports your requirement. Use the below code.

    public void RemoveWhiteSpace()
    {
    XmlDocument doc = new XmlDocument();
    doc.PreserveWhitespace= false;
    doc.Load("filename");
    doc.Save("filename");
    // Then start reading the save file
    }

    Fyi..
    If PreserveWhitespace is true before Load or LoadXml is called, white space nodes are preserved

    If PreserveWhitespace is true before Save is called, white space in the document is preserved in the output

    Hope this info helps


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.