SearchForKnowledge
SearchForKnowledge

Reputation: 3751

Why is the XML document not saving

I have the following string in my C# application:

string strData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
            strData += "<query:querySubmission>";
            strData += "    <submitter>";
            strData += "        <entityDBID>ghjgj</entityDBID>";
            strData += "        <vendorID>1fghfhfh</vendorID>";
            strData += "    </submitter>";
            strData += "    <payment>";
            strData += "        <creditCard>";
            strData += "            <number>4111111111111111</number>";
            strData += "            <expirationDate>2016-02-01</expirationDate>";
            strData += "            <cardholderName>JOE SMITH</cardholderName>";
            strData += "        </creditCard>";
            strData += "    </payment>";
            strData += "</query:querySubmission>";

I am trying to save it in XML format file with the spaces preserved. I did the following:

XmlDocument xm = new XmlDocument();
XmlTextWriter wr = new XmlTextWriter(@"C:\Users\EMWorks\Documents\text.xml",Encoding.UTF8);
wr.Formatting = Formatting.Indented;
xm.LoadXml(strData);
xm.Save(wr);
wr.Close();

I keep getting the following error:

'query' is an undeclared prefix

How can I fix it?

Upvotes: 0

Views: 874

Answers (3)

Lamourou abdallah
Lamourou abdallah

Reputation: 354

In order to use a namespace prefix (such as query:), the namespace must be declared, by writing xmlns:query='URI'.

        string strData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
        strData += "<query:querySubmission xmlns:query='ns'>";
        strData += "    <submitter>";
        strData += "        <entityDBID>ghjgj</entityDBID>";
        strData += "        <vendorID>1fghfhfh</vendorID>";
        strData += "    </submitter>"
        strData += "    <payment>";
        strData += "        <creditCard>";
        strData += "            <number>4111111111111111</number>";
        strData += "            <expirationDate>2016-02-01</expirationDate>";
        strData += "            <cardholderName>JOE SMITH</cardholderName>";
        strData += "        </creditCard>";
        strData += "    </payment>";
        strData += "</query:querySubmission>";
        //the using block is used to automatically dispose resources
        using (var wr= new XmlTextWriter(@"C:\test\text.xml", Encoding.UTF8))
        {
            wr.Formatting = Formatting.Indented;
            XmlDocument xm = new XmlDocument();
            xm.LoadXml(strData);
            xm.Save(wr);
        }

Upvotes: 0

Andrew
Andrew

Reputation: 5093

You need to declare the query namespace by adding xmlns:query="some URI" to your document.

You can see some extensive documentation here: http://www.w3.org/TR/REC-xml-names/

Upvotes: 2

helderdarocha
helderdarocha

Reputation: 23627

You need to declare a namespace which is represented by the query prefix. If you are using a schema from a specific application, it should be documented somewhere. Namespaces usually are declared as URIs (http://something).

If you just want to test the generation of the file, you can use some arbitrary namespace name such as ns1 (and change it later). You just need to add a xmlns declaration to the root element:

strData += "<query:querySubmission xmlns:query='ns1'>";

The prefix/namespace mapping will apply to the prefixed node where it is declared and to any prefixed descendants.

Upvotes: 2

Related Questions