Use XSD to determine elements and attributes in an XML file

Source: Internet
Author: User
As far as I know, ADO. Net does not seem to be able to implement this function. It uses XSD (schema) to judge the Data Type of the read node, but uses all the data as a string. The following class uses XSD to determine the data type of an element or attribute:

Using system;
Using system. IO;
Using system. xml;
Using system. xml. Schema;

Namespace REIT. aleis. xml
{
/// <Summary>
/// Reitxmldatavalidator is searched from an XSD File
/// Data type of an element or attribute and convert it to. NET data type
/// </Summary>
Public class reitxmldatatypefinder
{
Private XMLSCHEMA m_schema;

Public reitxmldatatypefinder (string xsdfilepath)
{
Filestream FS;
Try
{
FS = new filestream (xsdfilepath, filemode. Open );
M_schema = XMLSCHEMA. Read (FS, new validationeventhandler (showcompileerror ));
M_schema.compile (New validationeventhandler (showcompileerror); // compile it once
}
Catch
{
Throw;
}
}

Public System. Type findelementtype (string elementname)
{
System. xml. xmlqualifiedname name = new xmlqualifiedname (elementname, @ "http://www.reitweb.com/fc ");
Xmlschemaobject OBJ = m_schema.elements [name];
If (OBJ = NULL) return NULL;

Return findtype (OBJ );
}

Public System. Type findattributetype (string attributename)
{
System. xml. xmlqualifiedname attrname = new xmlqualifiedname (attributename, @ "http://www.reitweb.com/fc ");

Xmlschemaobject OBJ = m_schema.attributes [attrname];
If (OBJ = NULL) return NULL;

If (obj. GetType ()! = Typeof (xmlschemaattribute ))
Return NULL;

Xmlschemaattribute ATTR = (xmlschemaattribute) OBJ;
If (ATTR. attributetype! = NULL)
Return findtype (ATTR. attributetype );
Else
If (ATTR. schematypename. Name. compareto ("anytype") = 0) return typeof (object );

Return NULL;
}

Private Static void showcompileerror (Object sender, validationeventargs E)
{
Console. writeline ("validation error: {0}", E. Message );
}

private system. type findtype (Object OBJ)
{< br> If (obj. getType () = typeof (xmlschemasimpletype)
{< br> xmlschemasimpletype simpletype = (xmlschemasimpletype) OBJ;
return simpletype. datatype. valuetype;
}< br> else if (obj. getType () = typeof (xmlschemacomplextype)
{< br> xmlschemacomplextype complextype = (xmlschemacomplextype) OBJ;
return complextype. datatype. valuetype;
}< br> else
{< br> xmlschemadatatype datatype = (xmlschemadatatype) OBJ;
return datatype. valuetype;
}< BR >}

Posted on. NET refermer reading (2331) Comments (3) EDIT favorites reference favorites to 365key category: aleis Design


Comment:

# Re: Use XSD to judge the elements and attributes in the XML file at | what is the use of the xmlqualifiedname type ??

Not familiar with reply
# Re: Use XSD to determine the elements and attributes in the XML file. | Longping <? XML version = "1.0" encoding = "gb2312"?>
<Config>
<Field name = 'abscid' Title = 'absc No. 'inputbox = 'text' width = '000000' titlewidth = '000000'/>
<Field name = 'storeid' Title = 'database No. 'inputbox = 'text' width = '000000' titlewidth = '000000'/>
<Field name = 'uniteid' Title = 'uniform serial number 'inputbox = 'text' width = '000000' titlewidth = '000000'/>
<Field name = 'produproducingregion' Title = 'original' inputbox = 'text' width = '000000' titlewidth = '000000'/>
<Field name = 'breedregion' Title = 'conventional output' inputbox = 'text' width = '000000' titlewidth = '000000'/>
<List name = 'ref '>
<Column name = 'abscid'/>
<Column name = 'storeid'/>
<Column name = 'uniteid'/>
<Column name = 'produproducingare'/>
<Column name = 'breedare'/>
</List>
<Form name = 'Dialog 'Title = 'broad bean 'height = '000000' width = '000000'>
<Panel Cols = '2' border = '1'>
<Item name = 'abscid'/>
<Item name = 'storeid'/>
<Item name = 'uniteid'/>
<Item name = 'produproducingare'/>
<Item name = 'breedare'/>
</Panel>
</Form>
</Config>
Reply

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.