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?

7
  • possible duplicate of XmlSerializer: remove unnecessary xsi and xsd namespaces Commented Dec 22, 2011 at 7:35
  • @deathrace - what does the type look like, and what is the current xml looking like? Commented Dec 22, 2011 at 7: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 Commented Dec 22, 2011 at 7:55
  • @deathrace you really don't want Xmlns="...", for the simple reason that that isn't a valid namespace qualifier; it must be xmlns="..." Commented Dec 22, 2011 at 7:56
  • 1
    If I remove .Replace() method, It gives: <CallConnectReq xmlns:Xmlns="urn:intrado-com:ns:a9c" reqId="9" msgNb="2"/> Commented Dec 22, 2011 at 8:15

2 Answers 2

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 {}
Sign up to request clarification or add additional context in comments.

7 Comments

still it gives following output : <?xml version="1.0" encoding="utf-16"?> <CallConnectReq xmlns:Xmlns="urn:intrado-com:ns:a9c" reqId="9" msgNb="2" xmlns="xmlns=&quot;urn:intrado-com:ns:a9c&quot;"> <LocalCallId>0</LocalCallId> </CallConnectReq> I want to remove 'xmlns:' from this.
@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
Ohh, my mistake. That worked fine. actually problem was I was using XmlType instead of XmlRoot. Thank you very much.
just one problem here again. its adding q1: at each node: <q1:CallConnectReq xmlns="urn:intrado-com:ns:a9c" reqId="9" msgNb="2" xmlns:q1="xmlns=&quot;urn:intrado-com:ns:a9c&quot;"/>
@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
|
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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.