Friday, 3 April 2015

C# XML Deserialize in to Object - xmlns='' was not expected


<root element xmlns=''> was not expected.
{" was not expected.} XML Deserialize in C#

Change from standalone="yes"?> encoding="utf-8" ?>

Change from <?xml version="1.0" standalone="yes"?>  to <?xml version="1.0" encoding="utf-8" ?>

If you use Dataset.WriteXml("C:\\a.xml"); you will get  <?xml version="1.0" standalone="yes"?>

 objDataSet.WriteXml("C:\\a.xml");


If you use ..you will get <?xml version="1.0" encoding="utf-8" ?>

 XmlWriterSettings settings = new XmlWriterSettings();
                            settings.OmitXmlDeclaration = false;
                            XmlWriter objwriter = XmlWriter.Create("C:\\a.xml", settings);
                            objDataSet.WriteXml(objwriter);
                            objwriter.Close();

There are multiple root elements. Line 1, position 33

Solution:

Some where you are closing your element tag.
Ex:
Error: <Root/><Child>Hi</Child></Root>
Correct: <Root><Child>Hi</Child></Root>

C# XML read and remove namespace


DataSet WriteXML generates 'NewDataSet' root element



Solution:

 System.Data.DataSet myDs = new System.Data.DataSet("MyRoot");