0

I am trying to add namespace during Xml Serialization. So far I have added some of them but I am unable to add custom namespace using Xml serializer.

So far I have achieved this

   <?xml version="1.0"?>
<manifest xmlns:xmlns="http://www.imsproject.org/xsd/imscp_rootv1p1p2e" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_rootv1p2" xsi:schemaLocation="http://www.imsproject.org/xsd/imscp_rootv1p1p2 imscp_rootv1p1p2.xsd http://www.imsglobal.org/xsd/imsmd_rootv1p2p1 imsmd_rootv1p2p1.xsd http://www.adlnet.org/xsd/adlcp_rootv1p2 adlcp_rootv1p2.xs" identifier="test" version="2">
  <lom xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="test lom" xmlns="http://ltsc.ieee.org/xsd/LOM" />
  <resource adlcp:scormtype="sco" />
</manifest>

This is my Code

Manifest Class

[Serializable]
[XmlRoot(ElementName = "manifest")]
public class Manifest
{
    /// <summary>
    /// manifest root node
    /// </summary>
    [XmlAttribute(AttributeName = "identifier")]
    public string Identifier { get; set; }

    [XmlAttribute(AttributeName = "version")]
    public string Version { get; set; }

    //[XmlAttribute(AttributeName = "adlcp", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
    //public string Adlcp = "http://www.adlnet.org/xsd/adlcp_rootv1p2";

    [XmlAttribute(AttributeName = "schemaLocation", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
    public string SchemaLocation = "http://www.imsproject.org/xsd/imscp_rootv1p1p2 imscp_rootv1p1p2.xsd http://www.imsglobal.org/xsd/imsmd_rootv1p2p1 imsmd_rootv1p2p1.xsd http://www.adlnet.org/xsd/adlcp_rootv1p2 adlcp_rootv1p2.xs";


    [XmlElement(ElementName = "lom", Namespace = "http://ltsc.ieee.org/xsd/LOM")]
    public Lom Lom { get; set; }

    [XmlElement("resource")]
    public Resource Resource { get; set; }
}

Lom Class

 public class Lom
    {
        [XmlAttribute(AttributeName = "schemaLocation", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
        public string SchemaLocation { get; set; }

        [XmlAttribute(AttributeName = "xsi", Namespace = "http://www.imsproject.org/xsd/imscp_rootv1p1p2e")]
        public string Xsi = "http://www.w3.org/2001/XMLSchema-instance";


    }

Resource Class

 public class Resource
    {
        [XmlAttribute(AttributeName = "scormtype", Namespace = "http://www.adlnet.org/xsd/adlcp_rootv1p2")]
        public string ScormType { get; set; }
    }

My serialization function like this

using (FileStream fs = new FileStream(filePath, FileMode.Create))
        {
            var xmlSerializer = new XmlSerializer(typeof(Manifest));

        var ns = new XmlSerializerNamespaces();
        ns.Add("xmlns", "http://www.imsproject.org/xsd/imscp_rootv1p1p2e");
        ns.Add("adlcp", "http://www.adlnet.org/xsd/adlcp_rootv1p2");
        ns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
        xmlSerializer.Serialize(fs, data, ns);

        fs.Close();

    }

I need output like this.

<?xml version="1.0"?>
    <manifest 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    identifier="test" version="2" 
    xmlns:adlcp="test"
    xsi:schemaLocation="test location" 
    **xmlns="http://www.imsproject.org/xsd/imscp_rootv1p1p2"**>
      <lom 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://ltsc.ieee.org/xsd/LOM lom.xsd" 
      xmlns="http://ltsc.ieee.org/xsd/LOM" />
     <resource adlcp:scormtype="sco"/>
    </manifest>

what I am doing wrong?

3
  • how to achieve the desired output? Commented Mar 3, 2017 at 12:55
  • do you definitely need the repeat xsi namespace declaration on the lom sub element? I think that part is difficult to do without customising the serialisation, because the declaration is unnecessary (it's already declared on the parent). Commented Mar 3, 2017 at 13:23
  • The most immediate thing you need would be [XmlRoot(ElementName = "manifest", Namespace = "http://www.imsproject.org/xsd/imscp_rootv1p1p2")] ? also [XmlElement("resource")] should probably have an explicit Namespace Commented Mar 3, 2017 at 13:30

1 Answer 1

1

Simply add a namspace serializer

            var xmlSerializer = new XmlSerializer(typeof(Manifest), "http://www.w3.org/2001/XMLSchema-instance");
            var ns = new XmlSerializerNamespaces();
            ns.Add("xmlns", "http://www.w3.org/2001/XMLSchema-instance");
            xmlSerializer.Serialize(fs, data, ns);

and if data is defined as

        Manifest data = new Manifest()
        { Identifier = "test", Version = "2", Adlcp = "test", SchemaLocation = "test location" };
        data.Resource = new Resource() { ScormType="sco" };

with an attribute

    [XmlAttribute(AttributeName = "adlcp", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
    public string Adlcp { get; set; }

you'll get xmlns:adlcp="test" in the output

Same thing for

    [XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
    public string Xsi = "http://www.w3.org/2001/XMLSchema-instance";

As well as

            ns.Add("adlcp", "http://www.imsproject.org/xsd/Manifest/Adlcp");

with

    [XmlAttribute(AttributeName = "scormtype", Namespace = "http://www.imsproject.org/xsd/Manifest/Adlcp")]
    public string ScormType { get; set; }

to get a possible adlcp:scormtype="sco"

Add a namespace to the root class too, for example

[XmlRoot(ElementName = "manifest", Namespace = "http://www.imsproject.org/xsd/Manifest")]
public class Manifest
Sign up to request clarification or add additional context in comments.

4 Comments

I tried your code and its perfectly working except one namespace. I am getting two xmlns in root node like this xmlns:xmlns
I put same namespace but no luck any other idea?
any idea how to remove xmlns with prefix to just show only xmlns. I am currently have xmlns:xmlns
No, I'm afraid that this last thing is not possible in a simple and standard way

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.