Iain wrote:
I've created some c# classes from Xsds.
The classes have namespace attributes in the XmlRootAttributes.
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 XmlRootAttribute.
If you can't do that then using an XmlParserContext as follows does work
for me:
string xml = "<Foo><Bar>baz</Bar></Foo>";
XmlSerializer ser = new XmlSerializer(typeof(Foo));
NameTable nt = new NameTable();
XmlNamespaceManager mgr = new XmlNamespaceManager(nt);
mgr.AddNamespace("", "http://example.com/2008/ns1");
XmlParserContext ctxt = new XmlParserContext(nt, mgr, "",
XmlSpace.Default);
Foo foo = (Foo)ser.Deserialize(XmlReader.Create(new
StringReader(xml), null, ctxt));
Console.WriteLine("foo.Bar: {0}", foo.Bar);
where the Foo class is defined as follows:
[XmlRoot(Namespace = "http://example.com/2008/ns1")]
public class Foo
{
public string Bar { get; set; }
}
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/