As we all know, XML can be expanded. XML elements can be identified by name rather than by unknown. During XML deserialization, XML data can be converted into deserialized objects as long as the required information exists. However, this does not mean that the XML data used for deserialization must be the standard result of serialization. Sometimes XML data has redundant lengthy information, which is invisible by default during deserialization.
Xmlserializer has three events to detect unknown XML nodes: unknownnode, unknownelement, and unknownattribute. Corresponding to unknown nodes, unknown elements, and unknown attributes. Note that both XML elements and XML attributes belong to XML nodes.
For example:
Public Class Worker
{
Public IntID;
[Xmlattribute]
Public BoolRetired;
}
This only includes one element (ID) and attribute (retired), but there are other lengthy information in the XML data:
<?XML Version="1.0" Encoding="UTF-8"?>
<Worker Xmlns: xsi="Http://www.w3.org/2001/XMLSchema-instance"
Xmlns: XSD="Http://www.w3.org/2001/XMLSchema"
Retired="False"
Department="Sales">
<Name>Mgen</Name>
<ID>17</ID>
</Worker>
Register the corresponding event of xmlserializer and perform deserialization:
VaRXmlserializer= New Xmlserializer(Typeof(Worker));
Xmlserializer.Unknownnode+ =(Sender, E)=>
{
Console.Writeline ("Unknown node: {0} row {1} column {2 }", E.Name, E.Linenumber, E.Lineposition );
};
Xmlserializer.Unknownattribute+ =(Sender, E)=>
{
Console.Writeline ("Location Property: {0} row {1} column {2 }", E.ATTR.Name, E.Linenumber, E.Lineposition );
};
Xmlserializer.Unknownelement+ =(Sender, E)=>
{
Console.Writeline ("Location element: {0} row {1} column {2 }", E.Element.Name, E.Linenumber, E.Lineposition );
};
// Deserialize the XML file
VaRWorker=(Worker) Xmlserializer.Deserialize (File.Openread ("Xmlfile. xml"));
Output:
Unknown node: department row, column 5, column 21
Location attribute: department row, column 5, column 21
Unknown node: Name row, column 7, column 6
Location element: Name row, column 7, column 6