Amit Shah
Amit Shah

Reputation: 327

C# object to XML

I am creating an application which requires to convert c# object to XML.

I am using XML Serializer class to achieve this. Here is the code snippet:

public  class Anwer
{
    public int ID { get; set; }
    public string XML { get; set; }
    public Anwer(int ID, string XML)
    {
        this.ID = ID;
        this.XML = XML;
    }
    public Anwer() { }
}

Here is the main function:

   string AnswerXML = @"<Answer>1<Answer>";
   List<Anwer> answerList = new List<Anwer>();
   answerList.Add(new Anwer(1,AnswerXML));
   AnswerXML = @"<Answer>2<Answer>";
   answerList.Add(new Anwer(2, AnswerXML));
   XmlSerializer x = new XmlSerializer(answerList.GetType());
   x.Serialize(Console.Out, answerList);

The output is:

<?xml version="1.0" encoding="IBM437"?>
<ArrayOfAnwer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="h
ttp://www.w3.org/2001/XMLSchema">
  <Anwer>
    <ID>1</ID>
    <XML>&lt;Answer&gt;1&lt;Answer&gt;</XML>
  </Anwer>
  <Anwer>
    <ID>2</ID>
    <XML>&lt;Answer&gt;2&lt;Answer&gt;</XML>
  </Anwer>
</ArrayOfAnwer>

In the above code '<' and '>' are getting replaced by '<' and '&gt'; How to avoid this? I know string replace is one of the way, but I don't want to use it.

Thanks in advance.

Upvotes: 4

Views: 12379

Answers (6)

Mud
Mud

Reputation: 29021

I am creating an application which requires to convert c# object to XML. I am using XML Serializer class to achieve this

If you're using the XML Serializer to do the work, then why the "XML" field where you're inserting hand-coded XML? Seems like you want something more like this (using your class name, though it looks like a misspelling):

public class Anwer
{
    public int ID { get; set; }
    public int Answer { get; set; }
}

..

List<Anwer> answerList = new List<Anwer>() {
   new Anwer { ID=1, Answer=2 },
   new Anwer { ID=2, Answer=3 },
};
XmlSerializer x = new XmlSerializer(answerList.GetType());
x.Serialize(Console.Out, answerList);

..

<ArrayOfAnwer ...>
  <Anwer>
    <ID>1</ID>
    <Answer>2</Answer>
  </Anwer>
  ...

Or if you actually want/need the Answer element to be nested in an XML element for some reason, you can alter your Anwer object to reflect that structure (as Oleg Kalenchuk suggests), or generate the XML yourself rather than using the serializer:

XElement xml = new XElement("AnwerList", 
   from anwer in anwerList select
      new XElement("Anwer",
         new XElement("ID", anwer.ID),
         new XElement("XML",
            new XElement("Answer", anwer.Answer)
            )
         )
      );
Console.Out.WriteLine(xml);

<AnwerList>
  <Anwer>
    <ID>1</ID>
    <XML>
      <Answer>2</Answer>
    </XML>
  </Anwer>
  ...

I prefer the latter anyway, because it gives you more control.

Upvotes: 1

Peter
Peter

Reputation: 489

Because '<' and '>' are characters used for the xml-structure itself, they are automatically htmlencoded. When you read it back in your app and deserialize it, the '&lt;' and '&gt;' should be converted back to '<' and '>'.

If your goal is otherwise, use htmldecode functionality.

If this don't help, just tell what exactly you want to do with the xml-data.

Upvotes: 0

Oleg Kalenchuk
Oleg Kalenchuk

Reputation: 517

  1. Create a new class AnswerXML with one integer "Answer" member
  2. Change type of XML member to AnswerXML instead of string

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1063884

XmlSerializer won't believe you that an element is xml unless you convince it, for example by exposing that property as an XmlDocument. Otherwise, it (correctly, IMO) always encodes such values. For example:

using System;
using System.Xml;
using System.Xml.Serialization;
public class Anwer
{
    public int ID { get; set; }
    public XmlDocument XML { get; set; }
    public Anwer(int ID, string XML)
    {
        this.ID = ID;
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(XML);
        this.XML = doc;
    }
    public Anwer()
    { }
}
static class Program
{
    static void Main()
    {
        var answer = new Anwer(123, "<Answer>2</Answer>");
        var ser = new XmlSerializer(answer.GetType());
        ser.Serialize(Console.Out, answer);
    }
}

Upvotes: 5

Raj
Raj

Reputation: 1770

You're assigning a string containing the < and > sign to the XML element so it is obvious that teh serializer would replace the < and > with entity references. Even if you're getting > in the text when you deserialise the XML you'll get the > in your text.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1503140

You don't, basically. That's correctly serializing the object - the XML serializer doesn't want to have to deal with XML within strings messing things up, so it escapes the XML appropriately.

If you deserialize the XML later, you'll get back to the original object data.

If you're trying to build up an XML document in a custom fashion, I suggest you don't use XML serialization to start with. Either use LINQ to XML if you're happy to create elements etc explicitly, or if you really, really want to include arbitrary strings directly in your output, use XmlWriter.

If you could give us more information about the bigger picture of what you're trying to do, we may be able to suggest better alternatives - building XML strings directly is almost never a good idea.

Upvotes: 7

Related Questions