0

I searched and tried some attributes but none of them worked for my below scenario:

public class ObjSer
{
    [XmlElement("Name")]        
    public string Name
    {
        get; set;
    }
}

//Code to serialize
var obj = new ObjSer();
obj.Name = "<tag1>Value</tag1>";
        var stringwriter = new System.IO.StringWriter();
        System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());
        serializer.Serialize(stringwriter, obj);

Output would be as follows:

<ObjSer><Name>&lt;tag1&gt;Value&lt;/tag1&gt;</Name></ObjSer>

But I need output as:

<ObjSer><Name><tag1>Value</tag1></Name></ObjSer>

Scenario 2: In some cases I need to set:

obj.Name = "Value";

Is there any attribute or method I can override to make it possible?

4 Answers 4

1

You can't with default serializer. XmlSerializer does encoding of all values during serialization.

If you want your member to hold xml value, it must be XmlElement. Here is how you can accomplish it

public class ObjSer
{
    [XmlElement("Name")]
    public XmlElement Name
    {
        get; set;
    }
}

var obj = new ObjSer();

// <-- load xml
var doc = new XmlDocument();
doc.LoadXml("<tag1>Value</tag1>");
obj.Name = doc.DocumentElement;
// --> assign the element

System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());
serializer.Serialize(Console.Out, obj);

Output:

<?xml version="1.0" encoding="IBM437"?>
<ObjSer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Name>
    <tag1>Value</tag1>
  </Name>
</ObjSer>

UPDATE:

In case if you want to use XElement instead of XmlElement, here is sample on how to do it

public class ObjSer
{
    [XmlElement("Name")]
    public XElement Name
    {
        get; set;
    }
}

static void Main(string[] args)
{
    //Code to serialize
    var obj = new ObjSer();
    obj.Name = XDocument.Parse("<tag1>Value</tag1>").Document.FirstNode as XElement;
    System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());
    serializer.Serialize(Console.Out, obj);
}
Sign up to request clarification or add additional context in comments.

3 Comments

I would suggest to use XElement instead of XmlElement.
That will also do well.
Added Scenario 2, as some values from source contained no tag for same field. Suggested methods will throw error as it will not form the XML root. Thank you for efforts.
0

No, you can't. It is the natural and usual behaviour of the xml serializer. The serializer doesn't have to handle within the XML strings. So, it escapes the xml as expected.

You should decode the escaped string again while deserializing it.

If you want to add dynamically elements in the XML I suggest you to use Linq to XML and you can create tag1 or another kind of elements easily.

Comments

0

I would suggest serializing to an XDocument then converting that to a string and manually unescaping the desired part and writing it to a file. I would say this would give you the least headache it shouldn't be more than a couple lines of code. If you need it I can provide some code example if needed.

Comments

0

I found one more way of changing the type

public class NameNode
{
    public string tag1
    {
        get; set;
    }

    [XmlText]
    public string Value
    {
        get; set;
    }

}
public class ObjSer
{
    [XmlElement("Name")]        
    public NameNode Name
    {
        get; set;
    }
}

To set value of Name:

            var obj = new ObjSer();
            var valueToSet = "<tag1>Value</tag1>";
            //or var valueToSet = "Value";
            //With XML tag:
            if (valueToSet.Contains("</"))
            {
                var doc = new XmlDocument();

                doc.LoadXml(valueToSet);

                obj.Name.tag1 = doc.InnerText;
            }
            else //Without XML Tags
            {
                obj.Name.Value = senderRecipient.Name;
            }

This solution will work in both cases, but has limitation. It will work only for predefined tags (ex. tag1)

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.