We were given a sample document, and need to be able to reproduce the structure of the document exactly for a vendor. However, I’m a little lost with how C# handles namespaces. Here’s a sample of the document:
<?xml version='1.0' encoding='UTF-8'?> <Doc1 xmlns='http://www.sample.com/file' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://www.sample.com/file/long/path.xsd'> <header> <stuff>data</stuff> <morestuff>data</morestuff> </header> </Doc1>
How I’d usually go about this is to load a blank document, and then start populating it:
XmlDocument doc = new XmlDocument(); doc.LoadXml('<Doc1></Doc1>'); // Add nodes here with insert, etc...
Once I get the document started, how do I get the namespace and schema into the Doc1 element? If I start with the namespace and schema in the Doc1 element by including them in the LoadXml(), then all of the child elements have the namespace on them — and that’s a no-no. The document is rejected.
So in other words, I have to produce it EXACTLY as shown. (And I’d rather not just write text-to-a-file in C# and hope it’s valid XML).
You should try it that way
Do not forget to include the following namespaces: