locked
Unexpected XML declaration Error RRS feed

  • Question

  • User222199791 posted

    Hi 

    I'm getting an error validating my schema that says:

    Validation Message: Unexpected XML declaration. The XML declaration must be the first node in the document, and no white space characters are allowed to appear before it. Line 1, position 6.

    I've checked my xml file for whitespace and all  but I still can't seem to find the problem

    My xmlwriter code is as follow

    XmlWriterSettings settings = new XmlWriterSettings();
                    settings.Indent = true;
                    settings.NewLineOnAttributes = false;
                    XmlWriter wrt = XmlWriter.Create(url, settings);
                    wrt.WriteStartDocument();
                    wrt.WriteStartElement("OrderData", "http://www.assignment1.com");
                    wrt.WriteAttributeString("xmlns", "http://www.assignment1.com");
                    wrt.WriteStartElement("order");
                    wrt.WriteAttributeString("orderID", itemid);
                    wrt.WriteElementString("orderdate", date);
                    wrt.WriteElementString("buyerID", itemid);
                    wrt.WriteElementString("itemID", itemid);
                    wrt.WriteElementString("name", name);
                    wrt.WriteElementString("description", description);
                    wrt.WriteElementString("quantities", quantities);
                    wrt.WriteElementString("existingunitprice", existingunitprice);
                    wrt.WriteEndElement();
                    wrt.WriteEndElement();
                    wrt.Close();

    My xml:

    <?xml version="1.0" encoding="utf-8"?>
    <OrderData xmlns="http://www.assignment1.com">
      <order orderID="001">
        <orderdate>24/5/2015 12:00:00 AM</orderdate>
        <buyerID>001</buyerID>
        <itemID>001</itemID>
        <name>CPU</name>
        <description>This is a central processing unit</description>
        <quantities>Hardware</quantities>
        <existingunitprice>IBM</existingunitprice>
      </order>
    </OrderData>

    Any ideas on how I can get about fixing this problem?

    Sunday, May 24, 2015 6:54 AM

Answers

  • User1711366110 posted

    Any ideas on how I can get about fixing this problem?

       As per this case,you have to make sure your <?xml tag is the first thing in the document and it doesn't have anything before that.

    1.If your XML may include whitespace or comments like below:

    <!-- Import file: XmlDoc.xml -->
    
    <?xml version="1.0" encoding="utf-8"?>
    ...

    then change like below :

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

    2.You can have <?xml only once per document, so if you have a large chunk of XML and you have this tag repeated somewhere down the lines your document won't be valid.

    3.If still not working then you can use the below function for remove the unwanted white space :

    public static void DoRemovespace(string strFile)
        {
            string str = System.IO.File.ReadAllText(strFile);
            str = str.Replace("\n", "");
            str = str.Replace("\r", "");
            Regex regex = new Regex(@">\s*<");
            string cleanedXml = regex.Replace(str, "><");
            System.IO.File.WriteAllText(strFile, cleanedXml);
    
        }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, May 24, 2015 11:02 PM