Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a default namespace with no prefix using XMLSerializer

Tags:

I am trying to generate an XML document that contains the default namespace without a prefix using XmlSerializer, e.g.

<?xml version="1.0" encoding="utf-8" ?>
<MyRecord ID="9266" xmlns="http://www.website.com/MyRecord">
    <List>
        <SpecificItem>

Using the following code ...

string xmlizedString = null;
MemoryStream memoryStream = new MemoryStream();
XmlSerializer xs = new XmlSerializer(typeof(ExportMyRecord));
XmlSerializerNamespaces xmlnsEmpty = new XmlSerializerNamespaces();
xmlnsEmpty.Add(string.Empty, string.Empty);
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
xs.Serialize(xmlTextWriter, myRecord, xmlnsEmpty);
memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
xmlizedString = this.UTF8ByteArrayToString(memoryStream.ToArray());

and class structure ...

[Serializable]
[XmlRoot("MyRecord")]
public class ExportMyRecord
{
    [XmlAttribute("ID")]
    public int ID { get; set; }

Now, I've tried various options ...

XmlSerializer xs = new XmlSerializer
                     (typeof(ExportMyRecord),"http://www.website.com/MyRecord");

or ...

[XmlRoot(Namespace = "http://www.website.com/MyRecord", ElementName="MyRecord")]

gives me ...

<?xml version="1.0" encoding="utf-8"?>
<q1:MylRecord ID="9266" xmlns:q1="http://www.website.com/MyRecord">
    <q1:List>
        <q1:SpecificItem>

I need the XML to have the namespace without the prefix as it's going to a third party provider and they reject all other alternatives.

like image 526
OldBob Avatar asked Mar 23 '10 13:03

OldBob


People also ask

How do I change the default namespace in XML?

You can change the default namespace within a particular element by adding an xmlns attribute to the element. Example 4-4 is an XML document that initially sets the default namespace to http://www.w3.org/1999/xhtml for all the XHTML elements.

What is the default namespace The namespace used by default when no namespace is declared?

No namespace exists when there is no default namespace in scope. A {default namespace} is one that is declared explicitly using xmlns.

How do I add a namespace to an XML file?

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 the default namespace?

A default namespace is a namespace that does not include a prefix. The default prefix is applied to all the elements that do not include a prefix and is unique for different XMLports. For example, the following string specifies a namespace: urn:microsoft-dynamics-nav/xmlports/x100 , where 100 is the ID of the XMLport.


2 Answers

There you go:

ExportMyRecord instance = GetInstanceToSerializeFromSomewhere();
XmlSerializerNamespaces xmlnsEmpty = new XmlSerializerNamespaces();
xmlnsEmpty.Add(string.Empty, "http://www.website.com/MyRecord");
var serializer = new XmlSerializer(
    instance.GetType(), 
    "http://www.website.com/MyRecord"
);
like image 182
Darin Dimitrov Avatar answered Sep 24 '22 06:09

Darin Dimitrov


Here's a generic implementation that can be used for any type:

public static void Serialize<T>(T instance, string defaultNamespace, Stream stream)
{
    var namespaces = new XmlSerializerNamespaces();
    namespaces.Add(string.Empty, defaultNamespace);
    var serializer = new XmlSerializer(typeof(T), defaultNamespace);
    serializer.Serialize(stream, instance, namespaces);
}
like image 37
Sven Vranckx Avatar answered Sep 22 '22 06:09

Sven Vranckx