Deamination of comments in an XML file

I am trying to deserialize the following example XML file. I created a schema for this XML file. With a schema, I can deserialize the XML into an object.

But my problem is that I have XML comments (for example:) <!----Test-->in my XML.Deserializer file does not read XML comments on the object that I created using the schema.

And also I noticed that in the comment for comments node there is no entry available in the schema.

How can I read comments from an XML file on an object?

+3
source share
2 answers

- . XML . XMLSerializer , - .

, XmlReader ( @Amigable said), Deserialize(), XmlReader .

, , , , node, .

: XmlReader Deserialize:

:

XmlSerializer objSer = new XmlSerializer(typeof(CustomSchema));
StreamReader srmRdr = new StreamReader("Test.XML");
objForm = (CustomSchema)objSer.Deserialize(srmRdr);

.NETCF WM. ( XmlSerializer, .) .

, XmlReader Deserialize(), , , -, , , reset . , "Test.XML" XmlReader:

XmlReader xmlRdr = XmlReader.Create("Test.XML");

, :

    // Parse the file
    while (xmlRdr.Read())
    {
        switch (xmlRdr.NodeType)
        {
            case XmlNodeType.Element:
                // You may need to capture the last element to provide a context
                // for any comments you come across... so copy xmlRdr.Name, etc.
                break;
            case XmlNodeType.Comment:
                // Do something with xmlRdr.value
+2

, , , , , XmlReader, , XmlWriter ?

0

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


All Articles