C # object for XML

I am creating an application that requires converting a C # object to XML.

I am using the 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); 

Output:

 <?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 replaced by '<' and '& gt; How to avoid this? I know that replacing a string is one way, but I don't want to use it.

Thanks in advance.

+4
source share
6 answers

Mostly not. What the object serializes correctly is that the XML serializer does not want to deal with XML inside strings, confusing things, so it avoids XML accordingly.

If you deserialize the XML later, you will return to the original data of the object.

If you are trying to create an XML document in normal mode, I suggest you not use XML serialization for starters. Or use LINQ to XML if you are happy to create elements, etc. Explicitly, or if you really want to include arbitrary strings directly in your output, use XmlWriter .

If you could provide us with more information about the wider picture of what you are trying to do, we can offer better alternatives - directly creating XML strings is almost never a good idea.

+7
source

XmlSerializer will not believe you that the element is xml unless you convince it, for example, exposing this property as an XmlDocument . Otherwise, it (correctly, IMO) always encodes such values. For instance:

 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); } } 
+5
source

I am creating an application that requires converting a C # object to XML. I use the XML Serializer class to achieve this

If you use an XML serializer to do this work, then why the “XML” field into which you are inserting manually encoded XML code? It sounds like you want something more like this (using your class name, although it does look like spelling):

 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 really want / need a response element that can be embedded in an XML element for any reason, you can change your Anwer object to reflect this structure (as Oleg Kalenchuk suggests), or generate XML yourself, rather than use 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.

+1
source

You assign a string containing the <and> character to the XML element, so it is obvious that the serializer will replace the <and> with entity references. Even if you get> in text when deserializing XML, you will get text> in text.

0
source
  • Create a new AnswerXML class with one whole Answer element
  • Change XML member type to AnswerXML instead of string
0
source

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

If your goal otherwise, use the htmldecode function.

If this does not help, just let us know what exactly you want to do with the xml data.

0
source

Source: https://habr.com/ru/post/1332457/


All Articles