1

I'm trying to set xmlns attribute, inside the Document tag, value to a custom value using XmlSerializer. At the moment my simplified xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<Document xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03 pain.001.001.03.xsd">
  <GrpHdr>
    <Price Curency="EUR">
      40.55
    </Price>
  </GrpHdr>
</Document>

My simplified code looks like this:

public void Main(){
    var document = new CustomDocument();
    new XmlSerializer(typeof(CustomDocument)).Serialize(Console.Out, document);
}

[XmlRoot(ElementName = "Document")]
public class CustomDocument{

    [XmlAttribute("schemaLocation", AttributeName = "schemaLocation",
        Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
    public string SchemaLocation = "urn:iso:std:iso:20022:tech:xsd:pain.001.001.03 pain.001.001.03.xsd";

    [XmlElement("GrpHdr")
    Public XmlHeader {get; set;}

    Public XmlDocument(){
        XmlHeader = new XmlHeader();
    }
}

public class XmlHeader{
    [XmlElement("Price")
    Public string Price {get; set;}

    public XmlHeader(){
        Price = "40.55";
    }
}

How can I change the value of xmlns:xsd ?Adding [XmlElement("xmlns")] doesn't do the trick

Stralos
  • 4,895
  • 4
  • 22
  • 40
  • Similar question: http://stackoverflow.com/questions/2920142/how-to-add-xmlnamespace-to-a-xmldocument – anthares Oct 29 '15 at 12:01
  • Yes it is, but the person asking is using XmlDocument not XmlSerializer :) – Stralos Oct 29 '15 at 12:02
  • You can use the same approach after the XMLDocument instance is created and then serialize it. – anthares Oct 29 '15 at 12:04
  • Even if I could I don't think I want to. the Xml I'm genereting will be HUGE, and I did read that you should not use XMLDocument on big xml's, since it would cost you a lot of memory power. – Stralos Oct 29 '15 at 12:12

2 Answers2

2

Unfortunately you are not allowed to do that. The xmlns:xsi and xmlns:xsd are reserved namespaces. You can not change the default values as it's part of standard schema definition provided in .Net framework.

I'm not sure why you want to do that but if you want to add namespace matching namespace of your schema xsd then you can add a custom namespace like:

 [XmlRoot(ElementName = "Document", Namespace = "http://customNameSpaceFromXsd/XMLSchema.xsd")]
    public class CustomDocument{
    }

This will appear in your xml like:

<Document 
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://customNameSpaceFromXsd/XMLSchema.xsd"
xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03 pain.001.001.03.xsd">
vendettamit
  • 14,315
  • 2
  • 32
  • 54
1

Using XML Linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //<?xml version="1.0" encoding="utf-8"?>
            //<Document xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03 pain.001.001.03.xsd">
            //  <GrpHdr>
            //    <Price Curency="EUR">
            //      40.55
            //    </Price>
            //  </GrpHdr>
            //</Document>

            string xml =
               "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
               "<Document xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"urn:iso:std:iso:20022:tech:xsd:pain.001.001.03 pain.001.001.03.xsd\">" +
               "</Document>";

            XDocument doc = XDocument.Parse(xml);
            XElement document = (XElement)doc.FirstNode;
            XNamespace xsd = document.GetNamespaceOfPrefix("xsd");
            XNamespace xsi = document.GetNamespaceOfPrefix("xsi");

            document.Add(new XElement("GrpHddr",
                    new XElement("Price", new object[] {
                        new XAttribute("Currency", "EUR"),
                        40.55
                    })
            ));

        }
    }
}
​
jdweng
  • 33,250
  • 2
  • 15
  • 20