Problem Loading XML

  • Thread starter Thread starter vinoth
  • Start date Start date
V

vinoth

Hi,

I have created One Xml Structure using the following Code.

XmlDocument xd = new XmlDocument();
xd.LoadXml("<IPR><Test>123</Test><GSD>GSDINDIA</GSD></IPR>");
XmlElement xElem = xd.CreateElement("Personalization");
xElem.InnerXml = "<?xml version=\"1.0\"?><WrapClass
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><obj
xsi:type=\"DefinePersonalization\"><PersonalizationName>RestrictAccess</PersonalizationName><UserGroup><anyType
xsi:type=\"xsd:string\">administrator</anyType><anyType
xsi:type=\"xsd:string\">vinoth</anyType></UserGroup></obj></WrapClass>";
xd.DocumentElement.AppendChild(xElem);


Later i want to Load this XML. When i load this XML using LoadXml
method its giving the following error.

Error :

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.

How can i solve this problem? If anybody knows the solution please let
me know to solve it.


Thanks,
Vinoth
 
No, such declaration should be at the very beginning of the document.
If you want it, you should add it to xd.LoadXml.

xd.LoadXml("<?xml
version=\"1.0\"?><IPR><Test>123</Test><GSD>GSDINDIA</GSD></IPR>");
 
I guess you want to import a xml document into another xml document,
right?
If so, load 2 XmlDocument separately, then import a
XmlDocument.DocumentElement into the other XmlDocument using
XmlDocument.ImportNode, then append the imported node using AppendChild
method..
 
Hi,

XML declaration must be outside document element node and should be first
line of the xml. You cannot add xml declaration inside any other node. You
need to change your xElem.InnerXml value.

Mihir Solanki
http://www.mihirsolanki.com
 
Back
Top