deserialising xml files with no namespace

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?SWFpbg==?=

    deserialising xml files with no namespace

    I've created some c# classes from Xsds.

    The classes have namespace attributes in the XmlRootAttribut es.

    Accordingly, they will NOT deserialize with the XmlSerializer unless the
    same namespace is defined.

    I've prepared a temporary fix by loading the xml as an XmlDocument, amending
    the first element to include the namespace, serialising this to a string and
    deserialising to my classes.

    But surely there is an easier way....

    Iain
  • Martin Honnen

    #2
    Re: deserialising xml files with no namespace

    Iain wrote:
    I've created some c# classes from Xsds.
    >
    The classes have namespace attributes in the XmlRootAttribut es.
    >
    Accordingly, they will NOT deserialize with the XmlSerializer unless the
    same namespace is defined.
    >
    I've prepared a temporary fix by loading the xml as an XmlDocument, amending
    the first element to include the namespace, serialising this to a string and
    deserialising to my classes.
    >
    But surely there is an easier way....
    Well the easiest way is to change the XmlRootAttribut e.
    If you can't do that then using an XmlParserContex t as follows does work
    for me:

    string xml = "<Foo><Bar> baz</Bar></Foo>";
    XmlSerializer ser = new XmlSerializer(t ypeof(Foo));

    NameTable nt = new NameTable();
    XmlNamespaceMan ager mgr = new XmlNamespaceMan ager(nt);
    mgr.AddNamespac e("", "http://example.com/2008/ns1");
    XmlParserContex t ctxt = new XmlParserContex t(nt, mgr, "",
    XmlSpace.Defaul t);

    Foo foo = (Foo)ser.Deseri alize(XmlReader .Create(new
    StringReader(xm l), null, ctxt));
    Console.WriteLi ne("foo.Bar: {0}", foo.Bar);

    where the Foo class is defined as follows:

    [XmlRoot(Namespa ce = "http://example.com/2008/ns1")]
    public class Foo
    {
    public string Bar { get; set; }
    }


    --

    Martin Honnen --- MVP XML

    Comment

    • Joe Fawcett

      #3
      Re: deserialising xml files with no namespace

      "Iain" <Iain@discussio ns.microsoft.co mwrote in message
      news:D567B0A2-F2C7-47D9-9CAB-E3448349B00A@mi crosoft.com...
      I've created some c# classes from Xsds.
      >
      The classes have namespace attributes in the XmlRootAttribut es.
      >
      Accordingly, they will NOT deserialize with the XmlSerializer unless the
      same namespace is defined.
      >
      I've prepared a temporary fix by loading the xml as an XmlDocument,
      amending
      the first element to include the namespace, serialising this to a string
      and
      deserialising to my classes.
      >
      But surely there is an easier way....
      >
      Iain
      As well as Martin's suggestion why does the XML to be deserialised not have
      the namespaced elements?

      --

      Joe Fawcett (MVP - XML)




      Comment

      Working...