XML form control in ASP. NET

Source: Internet
Author: User
Tags xsl file

Import read: introduces ASP. NET, there is a small BUG: In WEBFORM. the XML control that appears in ASPX, where the transformsource attribute sets the path of the style table file, but this XSL file is not found in the source of the article. (Do it yourself.
--------------------------------------------------------------------------------
This code reveals Microsoft's ASP. a web form control hidden in the. NET architecture, that is, <asp: xml runat = server/>. I only give the code and don't explain it. Let's study it after class.
In addition, because it is beta1, you cannot use <xsl: sort> In the xslt you use in this control. Of course, you cannot use the order-, because it supports the xsl space with "1999", rather than the original one.
In addition, the answer I got from Microsoft is that in beta2, it will support <xsl: sort>, and you can switch all to xml + xsl, instead of having to worry about source code confidentiality.
See the following example:
Webform2.cs
-
Using System;
Using System. Collections;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Web;
Using System. Web. SessionState;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. HtmlControls;
Using System. Text;
Using System. IO;
Using System. Xml;

Public class WebForm2: Page
{
Public StringBuilder outputQ;
Public StringBuilder outputXml;
Public DocumentNavigator nav = null;
Public HtmlInputFile XmlFile;

Public System. Web. UI. WebControls. Xml MyXml;

Public System. Web. UI. WebControls. TextBox TextBox1;
Public System. Web. UI. WebControls. TextBox TextBox2;
Public System. Web. UI. WebControls. TextBox TextBox3;
Public System. Web. UI. WebControls. Button Query;
Public System. Web. UI. WebControls. Label FileLabel;

Public void On_KeyUp (object sender, System. EventArgs e)
{
Response. Write ("Works ");
}

Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
//
// Evals true first time browser hits the page
//
}
}

Public void Query_Click (object sender, System. EventArgs e)
{
HttpPostedFile xmlfile = XmlFile. PostedFile;
XmlDocument doc = new XmlDocument ();
MyXml. Document = new XmlDocument ();
// TextBox2.Text = "";
// TextBox3.Text = "";

If (xmlfile. FileName! = String. Empty)
{
Try
{
FileLabel. Text = xmlfile. FileName;

MyXml. Document. Load (xmlfile. FileName );
OutputXml = new StringBuilder ();
XmlTextReader reader = new XmlTextReader (xmlfile. FileName );
ShowDocument ();
TextBox3.Text = outputXml. ToString ();

OutputQ = new StringBuilder ();
Doc. Load (xmlfile. FileName );
DocumentNavigator nav = new DocumentNavigator (doc );
// Perform the query e.g. "descendant: book/price"
XPathQuery (nav, TextBox1.Text );
TextBox2.Text = outputQ. ToString ();

}
Catch (Exception exp ){
// OutputQ. Append ("</xmp> <font color =" # FF6600 ">" + exp. Message + "</font> <xmp> ");
}
Finally {}
}
Else if (FileLabel. Text! = String. Empty)
{
Try
{
MyXml. Document. Load (FileLabel. Text );
OutputXml = new StringBuilder ();
XmlTextReader reader = new XmlTextReader (FileLabel. Text );
ShowDocument ();
TextBox3.Text = outputXml. ToString ();

ShowDocument ();

OutputQ = new StringBuilder ();
Doc. Load (FileLabel. Text );
DocumentNavigator nav = new DocumentNavigator (doc );
// Perform the query e.g. "descendant: book/price"
XPathQuery (nav, TextBox1.Text );
TextBox2.Text = outputQ. ToString ();

}
Catch (Exception exp ){
OutputQ. Append ("</xmp> <font color =" # FF6600 ">" + exp. Message + "</font> <xmp> ");
}
Finally {}
}
}

Private void XPathQuery (XmlNavigator navigator, String xpathexpr)
{
Try
{
// Save context node position
Navigator. PushPosition ();
Navigator. Select (xpathexpr );
FormatXml (navigator );

// Restore context node position
Navigator. PopPosition ();
}
Catch (Exception e)
{
}
}

// ****************************** Navigator ******** ****************************
Private void FormatXml (XmlNavigator navigator)
{
While (navigator. MoveToNextSelected ())
{
Switch (navigator. NodeType)
{
Case XmlNodeType. ProcessingInstruction:
Format (navigator, "ProcessingInstruction ");
Break;
Case XmlNodeType. DocumentType:
Format (navigator, "DocumentType ");
Break;
Case XmlNodeType. Document:
Format (navigator, "Document ");
Break;
Case XmlNodeType. Comment:
Format (navigator, "Comment ");
Break;
Case XmlNodeType. Element:
Format (navigator, "Element ");
Break;
Case XmlNodeType. Text:
Format (navigator, "Text ");
Break;
Case XmlNodeType. Whitespace:
Format (navigator, "Whitespace ");
Break;
}
}
OutputQ. Append ("");
}

// Format the output
Private void Format (XmlNavigator navigator, String NodeType)
{
String value = String. Empty;
String name = String. Empty;

If (navigator. HasChildren)
{
Name = navigator. Name;
Navigator. MoveToFirstChild ();
If (navigator. HasValue)
{
Value = navigator. Value;
}
}
Else
{
If (navigator. HasValue)
{
Value = navigator. Value;
Name = navigator. Name;
}
}
OutputQ. Append (NodeType + "<" + name + ">" + value );
OutputQ. Append ("");
}

// *********************************** XmlReader *** **************************
Public void ShowDocument ()
{
OutputXml = new StringBuilder ();
XmlTextReader reader = new XmlTextReader (FileLabel. Text );

While (reader. Read ())
{
Switch (reader. NodeType)
{
Case XmlNodeType. ProcessingInstruction:
Format (reader, "ProcessingInstruction ");
Break;
Case XmlNodeType. DocumentType:
Format (reader, "DocumentType ");
Break;
Case XmlNodeType. Comment:
Format (reader, "Comment ");
Break;
Case XmlNodeType. Element:
Format (reader, "Element ");
Break;
Case XmlNodeType. Text:
Format (reader, "Text ");
Break;
Case XmlNodeType. Whitespace:
Break;
}
}
TextBox3.Text = outputXml. ToString ();
}

Protected void Format (XmlReader reader, String NodeType)
{
// Format the output
For (int I = 0; I <reader. Depth; I ++)
{
OutputXml. Append ();
}

OutputXml. Append (reader. Prefix + NodeType + "<" + reader. Name + ">" + reader. Value );

// Display the attributes values for the current node
If (reader. HasAttributes)
{
OutputXml. Append ("Attributes :");

For (int j = 0; j <reader. AttributeCount; j ++)
{
OutputXml. Append (reader [j]);
}
}
OutputXml. Append ("");
}

*********** **********************
Protected void ShowDocument (XmlNode node)
{
If (node! = Null)
Format (node );

If (node. HasChildNodes)
{
Node = node. FirstChild;
While (node! = Null)
{
ShowDocument (node );
Node = node. NextSibling;
}
}
}

// Format the output
Private void Format (XmlNode node)
{
If (! Node. HasChildNodes)
{
OutputXml. Append ("" + "<" + node. Value + "> ");
}

Else
{
OutputXml. Append ("<" + node. Name + "> ");
If (XmlNodeType. Element = node. NodeType)
{
XmlNamedNodeMap map = node. Attributes;
Foreach (XmlNode attrnode in map)
OutputXml. Append ("" + attrnode. Name + "<" + attrnode. Value + "> ");
}
OutputXml. Append ("");
}
}
}


The following figure shows webform2.aspx.
Webform2.aspx
---
<% @ Import Namespace = "System" %>
<% @ Import Namespace = "System. IO" %>
<% @ Assembly Name = "System. Xml" %>
<% @ Import Namespace = "System. Xml" %>
<% @ Page Language = "C #" Inherits = "WebForm2" Src = "WebForm2.cs" Debug = "true" %>

<HTML> <HEAD>

<Script runat = "server" language = "C #">
// Put page script here
Public void On_KeyUp (object sender, System. EventArgs e)
{
Response. Write ("Works ");
}

</Script>

<! -- <Link REL = "STYLESHEET" HREF = "de

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.