0

This is XML

<NS1:Response xmlns:NS1="http://www.opentravel.org/OTA/2003/05">
     <DATE CheckIn="2015-02-01"/>
</NS1:Response>

this Is Models

[Serializable]
[XmlRoot(ElementName = "Response")]
public class Response
{
    [XmlElement(ElementName = "DATE")]
    public DATE DATE {get;set;}

    public class DATE
    {
        [XmlAttribute(AttributeName = "CheckIn")]
        public string CheckIn {get;set;}
    }
}

How can I Add NS1 Namespace prefix in Models? Please advise me. Thank you very much.

user3444535
  • 233
  • 1
  • 2
  • 12

2 Answers2

2

Before I explain how to do what you want, it's important to realize that XML readers/parser generally don't care what prefixes you stick on your elements; they only care about the full namespace.

In other words, when your sample XML fragment is loaded, the ns1 bit is completely thrown away. Internally, what you get are XML namespace/element pairs, like ("http://www.opentravel.org/OTA/2003/05", "Response") or ("http://www.opentravel.org/OTA/2003/05", "Date"). The reason this is important to know is because you can assign different namespace prefixes to the XML data for use by e.g. XPath, and it will work fine. That is, I could read your XML fragment into my program, and say that "http://www.opentravel.org/OTA/2003/05" should map to the prefix "t", and use XPath like //t:Response to get the correct results, even though the source XML data had not t prefix.

In other words, you really, really should not bother trying to get a specific XML namespace prefix into your XML, because it should not matter. If having a specific prefix is necessary for everything to work properly, then someone somewhere is doing something very wrong.

Having said that, if for some reason you need to output specific namespace prefixes, or if you just happen to like the way they look, you can use the XmlSerializerNamespaces class, as so:

var ns = new XmlSerializerNamespaces(); 
ns.Add("NS1", "http://www.opentravel.org/OTA/2003/05");

var s = new XmlSerializer(typeof(Response));
var output = new StreamWriter(SOME_FILENAME);

s.Serialize(response, output, ns);

For this to work, you also have to decorate your classes with the full namespace you want them to be in. All of the XML Serializer attributes have a Namespace parameter you use for this purpose, e.g.:

[XmlRoot(ElementName = "Response",
         Namespace = "http://www.opentravel.org/OTA/2003/05")]
public class Response
{
}

When you serialize your object, the serializer will look up the namespaces in the namespace map, and apply the prefix you selected to the appropriate elements.

Michael Edenfield
  • 28,070
  • 4
  • 86
  • 117
  • I have tried to add XmlSerializerNamespaces as thors xml serializer, but writing prefixes does not work for types which are not using Serializable attribute, but they derive from IXmlSerializable interface. – Rudolf Dvoracek Jul 30 '18 at 08:01
  • With the IXmlSerializable interface I believe you can manually emit the namespace definitions using "writeattributestring" on the root element, and then include them as you write the elements out. See: https://stackoverflow.com/questions/7575218/namespace-prefixes-with-ixmlserializable – Michael Edenfield Jul 30 '18 at 14:11
  • @Michal Endelfield Writing attributes allows me to write attribute like xmlns:NS1="http://www.opentravel.org/OTA/2003/05", but **attribute is not a prefix** , see https://stackoverflow.com/questions/51590112/how-to-add-namespace-prefix-for-ixmlserializable-type – Rudolf Dvoracek Jul 30 '18 at 15:11
  • I'm not sure what you're asking; did you read the answer on the question I linked? In the root element of your document, a prefix _is_ an attribute (with a special format), and WriteElementString() allows you to specify both the namespace and namespace prefix for each element. – Michael Edenfield Jul 30 '18 at 15:58
  • From example you linked writing attribute manually it's possible to create , but I need to create , which is not possible with writing attribute. – Rudolf Dvoracek Jul 31 '18 at 08:54
0

The namespace prefix does not matter in XML. The namespace assigned to the prefix is the key.

You can use the XmlRoot attribute to assign the namespace.

[Serializable]
[XmlRoot(ElementName = "Response", 
         Namespace = "http://www.opentravel.org/OTA/2003/05")]
public class Response
Richard Schneider
  • 34,944
  • 9
  • 57
  • 73