2

I want to generate following xml output in my C# code :

<?xml version="1.0" encoding="utf-16"?>
<CallConnectReq Xmlns="urn:interno-com:ns:a9c" reqId="9" msgNb="2">
  <LocalCallId>0</LocalCallId>
</CallConnectReq>

right now I am achieving this as follows:

var xnameSpace = new XmlSerializerNamespaces();
                xnameSpace.Add("Xmlns", Constants.XmlNameSpaceValue);
                var xmlSerializer = new XmlSerializer(objToSerialize.GetType());
                var stringWriter = new StringWriter();
                xmlSerializer.Serialize(stringWriter, objToSerialize, xnameSpace);
                return stringWriter.ToString().**Replace("xmlns:","");**

But I want to remove "xmlns:" tag without using Replace() method. Is there any way to do it?

deathrace
  • 908
  • 4
  • 22
  • 48
  • possible duplicate of [XmlSerializer: remove unnecessary xsi and xsd namespaces](http://stackoverflow.com/questions/760262/xmlserializer-remove-unnecessary-xsi-and-xsd-namespaces) – jgauffin Dec 22 '11 at 07:35
  • @deathrace - what does the type look like, and what is the current xml looking like? – Marc Gravell Dec 22 '11 at 07:51
  • @jgauffin (my previous comment was incorrect, btw) - it is certainly *related*, but the syntax to get a default namespace is subtly different, and requires other changes to the type definition - sufficiently enough to be separate, IMO – Marc Gravell Dec 22 '11 at 07:55
  • @deathrace you really don't want `Xmlns="..."`, for the simple reason that that **isn't a valid namespace qualifier**; it **must** be `xmlns="..."` – Marc Gravell Dec 22 '11 at 07:56
  • actually I want to keep Xmlns="" as per my projects requirement. but I dont want default xmlns: – deathrace Dec 22 '11 at 08:14
  • 1
    If I remove .Replace() method, It gives: – deathrace Dec 22 '11 at 08:15
  • @deathrace.dj PLEASE double-check with whoever wrote the spec; `Xmlns` is **almost certainly** a typo in the spec, and has nothing to do with xml namespaces. The only way to add an `Xmlns` is via an `[XmlAttribute("Xmlns")] public string Foo {get;set;}` with value `"urn:blah"`, but note that it is **just** an arbitrary attribute value. – Marc Gravell Dec 22 '11 at 08:31

2 Answers2

4

To add just the default namespace:

var xnameSpace = new XmlSerializerNamespaces();
xnameSpace.Add("", "urn:interno-com:ns:a9c");
var ser = new XmlSerializer(typeof (CallConnectRequest));
ser.Serialize(destination, new CallConnectRequest(), xnameSpace);

with:

[XmlRoot("CallConnectReq", Namespace = "urn:interno-com:ns:a9c")]
public class CallConnectRequest {}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • still it gives following output : 0 I want to remove 'xmlns:' from this. – deathrace Dec 22 '11 at 08:12
  • @deathrace then you misapplied the change; you shouldn't have the `"Xmlns"` in `xnameSpace.Add`. It looks like you have: `xnameSpace.Add("Xmlns", "urn:blah")` - but that is **not** what I put in my answer – Marc Gravell Dec 22 '11 at 08:14
  • Ohh, my mistake. That worked fine. actually problem was I was using XmlType instead of XmlRoot. Thank you very much. – deathrace Dec 22 '11 at 08:20
  • 1
    just one problem here again. its adding q1: at each node: – deathrace Dec 22 '11 at 08:23
  • @deathrace.dj you have extra quotes in your Namespace one... I'm guessing you have `XmlRoot(..., Namespace="xmlns=\"blah\"")` - again, please compare to what I have in the example; **just** the namespace – Marc Gravell Dec 22 '11 at 08:28
  • then how can I get this namespace : Xmlns="urn:interno-com:ns:a9c" – deathrace Dec 22 '11 at 08:59
  • @deathrace.dj please, please take note: THAT IS NOT A NAMESPACE. It is just an attribute called `Xmlns` with value `urn:interno-com:ns:a9c`. Any similarity to namespaces is false. So; there are two options: a: you want a namespace, in which case it **must must must** be `xmlns` (and is handled by *this* answer), or b: you want an attribute - if so, see my *other* answer. – Marc Gravell Dec 22 '11 at 09:05
1

If you genuinely want Xmlns (which, to restate, I strongly believe is a typo of xmlns, and if not: is a bad choice in that it adds confusion), then:

var xnameSpace = new XmlSerializerNamespaces();
xnameSpace.Add("", "");
var ser = new XmlSerializer(typeof (CallConnectRequest));
ser.Serialize(destination, new CallConnectRequest {
    RequestId = 9,
    MessageNumber = 2,
    LocalCallId = 0
}, xnameSpace);

using:

[XmlRoot("CallConnectReq")]
public class CallConnectRequest {
    [XmlAttribute("Xmlns"), Browsable(false)]
    [EditorBrowsable(EditorBrowsableState.Never)]
    public string XmlNamespace {
        get { return "urn:interno-com:ns:a9c";} set { }
    }
    [XmlAttribute("reqId")]
    public int RequestId { get; set; }
    [XmlAttribute("msbNb")]
    public int MessageNumber { get; set; }

    [XmlElement("LocalCallId")]
    public int LocalCallId { get; set; }
}

which writes:

<?xml version="1.0" encoding="ibm850"?>
<CallConnectReq Xmlns="urn:interno-com:ns:a9c" reqId="9" msbNb="2">
  <LocalCallId>0</LocalCallId>
</CallConnectReq>
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900