Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a specific XML document using namespaces in C#

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).

like image 903
Clinton Pierce Avatar asked Jan 14 '09 14:01

Clinton Pierce


People also ask

How do you create a namespace in XML?

XML Namespaces - The xmlns Attribute When using prefixes in XML, a namespace for the prefix must be defined. The namespace can be defined by an xmlns attribute in the start tag of an element. The namespace declaration has the following syntax. xmlns:prefix="URI".

What is XML namespace explain with example?

An XML namespace is a collection of names that can be used as element or attribute names in an XML document. The namespace qualifies element names uniquely on the Web in order to avoid conflicts between elements with the same name.

Can a single XML document have elements from multiple namespaces?

Declaration scope When you use multiple namespaces in an XML document, you can define one namespace as the default namespace to create a cleaner looking document. The default namespace is declared in the root element and applies to all unqualified elements in the document.

What is namespace in XML schema?

Figure 1: Elements and attributes in XML Schema namespace are used to write an XML Schema document, which generates elements and attributes as defined by user and puts them in {target namespace}. This {target namespace} is then used to validate the XML instance.


2 Answers

You should try it that way

  XmlDocument doc = new XmlDocument();  

  XmlSchema schema = new XmlSchema();
  schema.Namespaces.Add("xmlns", "http://www.sample.com/file");

  doc.Schemas.Add(schema);

Do not forget to include the following namespaces:

using System.Xml.Schema;
using System.Xml;
like image 119
Dimi Takis Avatar answered Sep 19 '22 00:09

Dimi Takis


I personally prefer to use the common XmlElement and its attributes for declaring namespaces. I know there are better ways, but this one never fails.

Try something like this:

xRootElement.SetAttribute("xmlns:xsi", "http://example.com/xmlns1");
like image 24
mathifonseca Avatar answered Sep 20 '22 00:09

mathifonseca