Xml xmlschema (XSD) learning experience and XML formatting and verification Summary

Source: Internet
Author: User

I have been learning and using XML as a definition language for some time. As the complexity of my XML definition increases, I am eager to implement code prompts and verification in vs2010.

I was exposed to XMLSCHEMA last week to solve my own puzzles. After a week of hard work, I finally completed the prompts and verification of my XML-defined code. I am very grateful to the netizens for sharing this article, summarize it as a return.

1. XML definition

<? XML version = "1.0" encoding = "UTF-8"?>
<Root>
<Myquery Title = "Import employee data" isvisible = "true">
<URL> TMP/myimport. aspx? N = importhbinfo1 & amp; M = d </URL>
</Myquery>
</Root>

2. XMLSCHEMA (open in vs2010 and generate XMLSCHEMA based on your xml file in the XML menu)

<? XML version = "1.0" encoding = "UTF-8"?>
<Xs: schema attributeformdefault = "unqualified" elementformdefault = "qualified" xmlns: xs = "http://www.w3.org/2001/XMLSchema">
<Xs: element name = "root">
<Xs: complextype>
<Xs: sequence>
<Xs: Element maxoccurs = "unbounded" name = "myquery">
<Xs: complextype>
<Xs: sequence>
<Xs: element name = "url" type = "XS: string"/>
</Xs: sequence>
<Xs: attribute name = "title" type = "XS: string" use = "required"/>
<Xs: attribute name = "isvisible" type = "XS: string" use = "required"/>
</Xs: complextype>
</Xs: Element>
</Xs: sequence>
</Xs: complextype>
</Xs: Element>
</Xs: schema>

3. Expand the XMLSCHEMA above to make it more in line with his own needs (you can understand it at a glance and Google it if you don't understand it)

<? XML version = "1.0" encoding = "UTF-8"?>
<Xs: schema attributeformdefault = "unqualified" elementformdefault = "qualified" xmlns: xs = "http://www.w3.org/2001/XMLSchema"

Targetnamespace = "http: // 121.18.78.216" xmlns = "http: // 121.18.78.216">

<! -- Add your namespace -->
<Xs: element name = "root">
<Xs: complextype>
<Xs: sequence>
<Xs: Element minoccurs = "1" maxoccurs = "unbounded" name = "myquery">
<Xs: complextype>
<Xs: sequence>
<Xs: Element minoccurs = "1" maxoccurs = "unbounded" name = "url" type = "urltype"/>
</Xs: sequence>
<Xs: attribute name = "title" type = "XS: string" use = "required"/>
<Xs: attribute name = "isvisible" type = "logicvalue"/>
</Xs: complextype>
</Xs: Element>
</Xs: sequence>
</Xs: complextype>
</Xs: Element>

<! -- Define your own type -->
<Xs: simpletype name = "urltype">
<Xs: Restriction base = "XS: string">
<Xs: Pattern value = "(http ://)? ([\ W-] + \.) * [\ W-] * (/? [_ \ W -./? % & Amp; =] *)? "> </Xs: Pattern>
</Xs: Restriction>
</Xs: simpletype>
<Xs: simpletype name = "logicvalue">
<Xs: Restriction base = "XS: string">
<Xs: enumeration value = "false"/>
<Xs: enumeration value = "true"/>
</Xs: Restriction>
</Xs: simpletype>
</Xs: schema>

In this way

<Root xmlns = "http: // 121.18.78.216" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance">

You can implement prompt and automatic verification in vs2010.

4. C # code verification

Private Static string msgcheck = NULL;
/// <Summary>
/// Verify the XML file or string based on demo. XSD
/// </Summary>
/// <Param name = "XML"> the XML file must end with. xml or a string of XML. </param>
/// <Returns> </returns>
Public static string checkxmlcontent (string XML)
{
// Initialization error message
Msgcheck = string. empty;
Try
{
String xsdfile = webhelper. getmyxmlpath () + "\ demo. XSD"; // XSD architecture document
If (file. exists (xsdfile ))
{
Xmlschemaset xssarchcontent = new xmlschemaset (); // construct schema Cache
Xssarchcontent. Add ("http: // 121.18.78.216", xsdfile); // Add the schema file, which is prefixed with the namespace and is not empty
Xmlreadersettings xrsarchcontent = new xmlreadersettings (); // defines how to use the document mode.
Xrsarchcontent. validationtype = validationtype. Schema;
Xrsarchcontent. schemas = xssarchcontent; // associate the reader with the schema set.
// Add the event handler when an error occurs
Xrsarchcontent. validationeventhandler + = new validationeventhandler (dealvalidationerror );
// Construct a reader that can be verified and construct a verification Reader
Xmlreader xrarchcontent = NULL;
If (XML. endswith (constants. xml_suffix, stringcomparison. currentcultureignorecase ))
{
Xrarchcontent = xmlreader. Create (XML, xrsarchcontent );
}
Else
{
Xrarchcontent = xmlreader. Create (New stringreader (XML), xrsarchcontent );
}
// All document nodes of cyclic Detection
While (xrarchcontent. Read ())
{
If (xrarchcontent. nodetype = xmlnodetype. Element
& Msgcheck. indexof ("# nodename #")! =-1)
{
Msgcheck = msgcheck. Replace ("# nodename #", xrarchcontent. Name );
}
}
Xrarchcontent. Close ();
}
}
Catch (exception ex)
{
Msgcheck + = ex. message;
}
Return msgcheck. Replace ("# nodename #", String. Empty );
}
/// <Summary>
/// Error handling program
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "ARGs"> </param>
Private Static void dealvalidationerror (Object sender, validationeventargs ARGs)
{
Msgcheck + = "# nodename #" + args. Message + environment. newline;
}
5. XML Code uses xmldocument to generate a row and provides a formatting method.

/// <Summary>
/// Return the formatted XML document
/// </Summary>
/// <Param name = "Doc"> XML Document Object </param>
/// <Returns> </returns>
Public static string getformatxml (xmldocument DOC)
{
String result = NULL;
If (Doc! = NULL)
{
Try
{
Memorystream mstream = new memorystream (1024 );
Xmltextwriter writer = new xmltextwriter (mstream, null );
Writer. Formatting = formatting. indented;
Doc. writeto (writer );
Writer. Flush ();
Writer. Close ();

Result = encoding. utf8.getstring (mstream. toarray ());
Mstream. Close ();
}
Catch
{
Result = getformatxml (Doc. outerxml );
}
}
Return result;
}
/// <Summary>
/// Return the formatted XML string (new line feed)
/// </Summary>
/// <Param name = "XML"> XML string </param>
/// <Returns> </returns>
Public static string getformatxml (string XML)
{
If (string. isnullorempty (XML ))
{
Return NULL;
}
Else
{
Return XML. Replace ("> \ r \ n", ">"). Replace (">", "> \ r \ n ");
}
}

Log on to http: // 121.18.78.216/

 

 

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.