. Net (C #): aware of unknown nodes in XML deserialization

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.