Click here to Skip to main content
14,935,565 members
Please Sign up or sign in to vote.
5.00/5 (3 votes)
See more:
Hi There,
I doing a project that uses XML Serialization to open existing XML files. It turns out that the XML's that my project loads are missing a namespace declaration.
The program that creates the XML's uses this "xlink" namespace but it never defines it. Because of this missing namespace declaration when my code tries to deserialize the XML it throws an exception. Is it possible to define this "xlink" namespace before I try to deserialize the XML document so that it won't throw an exception?

My Code:
XmlSerializer pageDeserializer = new XmlSerializer(typeof(ContentPage));
TextReader pageReader = new StreamReader(pathToFolder + "\\" + Resources.DEFAULT_PAGE_name);
this.contentPage = (ContentPage)pageDeserializer.Deserialize(pageReader);



Example XML Doucment that needs to be deserialized:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><svg width="768" height="768"><image x="0" y="0" width="768" height="768" xlink:href="images/mask.png"/></svg>
Posted

1 solution

Hi,

You could get this working using a XmlReader and setting the used namespaces in a XmlParserContext / XmlNamespaceManager.

Here is the example code I used to test:
string xml = @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?>
		<svg width=""768"" height=""768"">
		<image x=""0"" y=""0"" width=""768"" height=""768"" xlink:href=""images/mask.png""/>
				</svg>";

XmlSerializer pageDeserializer = new XmlSerializer(typeof(svg));
			
using (TextReader txReader = new StringReader(xml))
{
	// Create XmlReaderSettings
	XmlReaderSettings settings = new XmlReaderSettings();
	settings.ConformanceLevel = ConformanceLevel.Fragment;
	settings.IgnoreWhitespace = true;
	settings.IgnoreComments = true;

	// Create a new NameTable
	NameTable nt = new NameTable();

	// Create a new NamespaceManager
	XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);

	// Add your namespaces used in the XML
	nsmgr.AddNamespace("xlink", "urn:http://namespaceurl.com");

	// Create the XmlParserContext using the previous declared XmlNamespaceManager
	XmlParserContext ctx = new XmlParserContext(null, nsmgr, null, XmlSpace.None);

	// Instantiate a new XmlReader, using the previous declared XmlReaderSettings and XmlParserContext
	XmlReader reader = XmlReader.Create(txReader, settings, ctx);

	// Finally, deserialize
	svg deserialize = (svg) pageDeserializer.Deserialize(reader);
}


Hope this helps.

Best regards and happy coding,
Stops
   
v2
Comments
amura.cxg 18-Jul-11 16:15pm
   
Thanks sTOPs!
It works perfectly, I really appreciate the help
Christoph Keller 18-Jul-11 16:20pm
   
You're welcome! I'm glad to hear that it worked! :)

Have a nice day :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900