1

Reference SO Ques

I have 2 Xml's which I want to deserialize into 1 class as the innerxml is same, I want to remove the Scehma ie the namespaces and want to change rootname before deserialising.

XML 1

<MyXMLTest1 xsi:schemaLocation="abc1" xmlns:xsi="abc12" xmlns="abc123"> <element1>xml1hi</element1> </MyXMLTest1>

XML 2

<MyXMLTest2 xsi:schemaLocation="abc2" xmlns:xsi="abc23" xmlns="abc234"> <element1>xml1hi</element1> </MyXMLTest2>

Class

[System.Xml.Serialization.XmlRoot("MyXMLTest", IsNullable=true)] public partial class MyXMLTest{ public string element1 {get;set;} }

Method

var xDocument = XDocument.Load(@"myxml.xml"); string myxmlstring = xDocumnet.ToString(); var reader = new StringReader(myxmlstring ); var serializer = new XmlSerializer(typeof(MyXMLTest)); var instance = (MyXMLTest)serializer.Deserialize(reader); //I'm getting error on the last line that <MyXMLTest1 xsi:schemaLocation="abc1" xmlns:xsi="abc12" xmlns="abc123"> was not expected

How Can I then ignore the namespaces and the Root Name for any uploaded Xml?

Thanks

Community
  • 1
  • 1
Arijit Mukherjee
  • 3,817
  • 2
  • 31
  • 51

2 Answers2

1

Finally I Figured Out the Solution for this Issue

What I did is

  • I wrote a method to Change/Override the RootName for every incoming XML
  • To remove the Namespaces

and then use them.

Example is here:

hope someone will find it useful.

Class

[System.Xml.Serialization.XmlRoot("GenericXMLClass", IsNullable=true)]
public partial class GenericXMLClass
{
    public string element1 {get;set;}
}

Functions

//RemoveNameSpaces
    public class NamespaceIgnorantXmlTextReader : XmlTextReader
    {
        public NamespaceIgnorantXmlTextReader(System.IO.TextReader reader) : base(reader) { }

        public override string NamespaceURI
        {
            get { return ""; }
        }
    }
    //RemoveNameSpaces
    public class XTWFND : XmlTextWriter
    {
        public XTWFND(System.IO.TextWriter w) : base(w) { Formatting = System.Xml.Formatting.Indented; }
        public override void WriteStartDocument() { }
    }

    //Generate Generic XML Files
    public GenericXMLCLass GenericXmlGenerator()
    {
        var xDocument = XDocument.Load(@"myxml.xml"); 
        string xmlString = xDocumnet.ToString();
        GenericXMLCLass genericXML = new GenericXMLCLass();
        String xml;
            XmlDocument objDoc = new XmlDocument();
            objDoc.LoadXml(xmlString);
            XmlDocument objNewDoc = new XmlDocument();
            XmlElement objNewRoot = objNewDoc.CreateElement("GenericXMLClass");
            objNewDoc.AppendChild(objNewRoot);
            objNewRoot.InnerXml = objDoc.DocumentElement.InnerXml;

            xml = objNewDoc.OuterXml;

                var serializer2 = new XmlSerializer(typeof(GenericXMLClass));
                var reader = new StringReader(xml);
                var instance = (GenericXMLClass)serializer2.Deserialize(new NamespaceIgnorantXmlTextReader(reader));
                genericXML = instance;

        }
        return genericXML;
    }
Arijit Mukherjee
  • 3,817
  • 2
  • 31
  • 51
0

Given what you appear to be doing here is deserializing XML fragments perhaps you should look at using XElement, it does a good job of parsing XML irrespective of namespace/root names etc.

var xml = "<MyXMLTest1 xsi:schemaLocation='abc1' xmlns:xsi='abc12' xmlns='abc123'><element1>xml1hi</element1></MyXMLTest1>";
var xmlTest = new MyXMLTest
{
    element1 = XElement.Parse(xml).Value
};
Console.WriteLine(xmlTest.element1); // outputs "xml1hi"

...there are 100 of elements

That's not a problem, that's where the beauty of LINQ to XML comes in, you can query the XML and project each element1 as a new MyXMLTest instance

var fragments = from el in XElement.Parse(xml).Elements()
                where el.Name.LocalName == "element1"
                select new MyXMLTest
                {
                    element1 = el.Value
                };

foreach (var f in fragments)
{
    Console.WriteLine(f.element1);
}
James
  • 80,725
  • 18
  • 167
  • 237
  • This xml was just a sample there are 100 of elements, so mentioning them will not be a good idea i think. Plus I have Nested Classes inside my xml and xmlclass – Arijit Mukherjee Aug 13 '14 at 09:31
  • @ArijitMukherjee if there are more than 1 element then you can project each one as a new `MyXMLTest` instance (see update). – James Aug 13 '14 at 09:43
  • James, My XMLTest class another class objects as well. Like `public XMLTEST { public string element1{get;set;} public mymessages _allMessages {get;set;} }` Hope I make clear myself. – Arijit Mukherjee Aug 13 '14 at 09:46
  • @ArijitMukherjee sorry but I can only go on what information you have provided in your initial question, you should disclose all relevant information in the initial question and when giving examples try to be as close to the real code as possible - otherwise it's just a guessing game. Regardless, I think my answer has given you a solution to the core problem, it's up to you to do the rest. – James Aug 13 '14 at 09:57
  • http://stackoverflow.com/questions/2296305/deserialize-xml-without-namespaces-but-in-a-class-expecting-namespaces This is some what My problem is plus in the newer version my file name has also changed. ANyways thanks alot and devoting your time. – Arijit Mukherjee Aug 13 '14 at 10:04