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() ?

7
  • The following may be helpful: post and this post is VB.NET, but explains how to figure out the structure of the classes. Commented Jun 22, 2022 at 15:47
  • 2
    Why not deserialize the list? dotnetfiddle.net/gh2SAT Commented Jun 22, 2022 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 ? Commented Jun 22, 2022 at 22:18
  • 1
    deserialization only works if the xml is valid, so I don't see a reason to validate it yourself Commented Jun 23, 2022 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 Commented Jun 23, 2022 at 10:53

1 Answer 1

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; }
}
Sign up to request clarification or add additional context in comments.

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.