Search code examples
c#xmlxml-serialization

Insert a tag in the serialization of an xml file


I am serializing an XML file with the classes created automatically with paste special of an XML. I did everything without problems, more than 3,000 lines (and the Xml file more than 600), but I have a tag that I can't serialize. In the original xml file I have this:

<sdtc:statusCode code="active"/>

This is the class generated by Visual Studio:

public string code
{
    get { return this.codeField; }
    set { this.codeField = value; }
}

I serialize it like this:

statusCode oStatusCode = new statusCode();
oStatusCode.code = "active";

And this is the serialized result:

<statusCode code="active" />

As you can see, the sdtc: prefix is missing and I don't know how to insert it.

I tried with:

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:hl7-org:sdtc")]

but it has no effect. I searched for hours but couldn't find a solution. Unfortunately, this is my first experience with serialization.

Any ideas or suggestions?

EDIT: Since the serialization classes are more than 12,000 lines long, I don't think I can insert them, I'll try to insert something useful, hope I'm not wrong. First line of XML file:

<ClinicalDocument xmlns="urn:hl7-org:v3" xmlns:mif="urn:hl7-org:v3/mif" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:lab="urn:oid:1.3.6.1.4.1.19376.1.3.2" xmlns:sdtc="urn:hl7-org:sdtc">

The first lines of serializer code:

[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:hl7-org:v3")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "urn:hl7-org:v3", IsNullable = false)]

Solution

  • This is my first experience with the serializer but having to solve the problem I used this solution, certainly incorrect and inelegant, but it works.

            string cRead = File.ReadAllText("mycda.xml");
            System.Xml.Linq.XDocument xdoc = System.Xml.Linq.XDocument.Parse(cRead);           
            string cNew = xdoc.ToString();
            cNew = cNew.Replace("<statusCode code=\"activemod\" />", "<sdtc:statusCode code=\"active\" />");
    

    (I use the activemod value to distinguish it from all the others)