0

I'm trying to loop into an XmlDocument to serialize objects. Let's suppose an simple xml :

<?xml version="1.0" encoding="iso-8859-15"?>
<root>
    <message>
        <id>1</id>
        <text>test</text>
    </message>
    <message>
        <id>2</id>
        <text>test 2</text>
    </message>
</root>

So this is my c# program :

class Program
{
    static void Main(string[] args)
    {
        XmlReaderSettings readerSettings = new XmlReaderSettings();
        readerSettings.IgnoreComments = true;

        XmlSerializer serializer = new XmlSerializer(typeof(Message));

        XmlReader xmlReader = XmlReader.Create(@"..\..\test.xml");
        XmlDocument doc = new XmlDocument();
        doc.Load(xmlReader);



        foreach(XmlElement element in doc.DocumentElement.ChildNodes)
        {
            Console.WriteLine($"id : {element.SelectSingleNode("id").InnerText}, message : {element.SelectSingleNode("text").InnerText}");
            Message message = (Message)serializer.Deserialize(XmlReader.Create(element.OuterXml.ToString()));
        }

        Console.ReadLine();
    }
}

public class Message
{
    public int id;
    public string text; 
}

but i got an error Illegal characters in path, but the print is okay, what's wrong ? and is there a way to serialize directly the XmlElement without go through the tostring() ?

  • The following may be helpful: [post](https://stackoverflow.com/questions/72587699/too-many-xml-in-one-xml-file/72589790#72589790) and this [post](https://stackoverflow.com/questions/68510288/edit-tag-parameter-in-xml-using-vb-net/68513150#68513150) is VB.NET, but explains how to figure out the structure of the classes. – Tu deschizi eu inchid Jun 22 '22 at 15:47
  • 2
    Why not deserialize the list? https://dotnetfiddle.net/gh2SAT – Rand Random Jun 22 '22 at 15:51
  • thanks for your response. In my case, it's a data retrieval from an old system. So i thought to tcheck if the file is okey by loading the xml and after i need to loop on messages and tcheck some conditions on the message before saving it on db if tchecks are ok. That why i wanted to know how to deserialize a specific XmlElement. any idea ? – Lucas Weibel Jun 22 '22 at 22:18
  • please writ comments with @username so users will get notified – Rand Random Jun 23 '22 at 10:49
  • 1
    deserialization only works if the xml is valid, so I don't see a reason to validate it yourself – Rand Random Jun 23 '22 at 10:52
  • 1
    if you want to validate the content the Text of a message, I would recommend doing it on the message objects after the deserialization and just simple remove invalid messages from the list – Rand Random Jun 23 '22 at 10:53
  • thanks for your response @RandRandom, that would be a good advice, but in my case, i was doing a try catch and adding some info into a log file for each error of serialization. – Lucas Weibel Jun 26 '22 at 18:19

1 Answers1

0

Ok, i found the problem. The serializer is looking for a tag named Message like the name of the class but the tag was message with an lowercase m. It is still possible to differentiate the name of the class and the label of the tag and associate them with a decorator as :

[XmlRoot(ElementName = "message")]
public class Message
{
    public int id { get; set; }
    public string? text { get; set; }
}