Use XML in DOTNET

Source: Internet
Author: User
Tags dotnet xsl
Haha, it's interesting. I heard a joke that an old German man was a deaf man. When he went to the bathroom, he suddenly attacked Soviet planes. Even though the house collapsed, the whole family had nothing to do, my family opened the wall of the bathroom and found the old man happy there, saying: As soon as I pulled the rope of the toilet, the house fell down.
Hahaha, that's the same for me. Just now I sent this post, and chinaasp was finished. I thought it was my business. It was originally a Soviet plane that had blown up.
If you like to play with xml, please come with me, but before that, please take a deep breath, because I will introduce you to a long code, this code reveals Microsoft in 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 in beta2, which will support <xsl: sort>. By then, my brother will switch all to xml + xsl, it is a headache for 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 ("\ r \ n ");
}

// 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 ("\ r \ n ");
}

// *********************************** 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 ('\ t ');
}

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 ("\ r \ n ");
}

*********** **********************
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 ("\ t" + "<" + 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 ("\ r \ n ");
}
}
}

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 = "default.css" TYPE = "text/css"> -->
<TITLE> test </TITLE>
</HEAD>

<BODY>

<Form method = "post" action = "WebForm2.aspx" runat = "server" enctype = "multipart/form-data">

<Div align = "left">
<Table>
<Tr>
<Td> XML Document: </td>
<Td> <input type = file id = "XmlFile" runat = server> FileName: </td>
<Td> <asp: label id = "FileLabel" runat = "server"> </asp: label> </td>
</Tr>

<Tr>
<Td> XPath Expression </td>
<Td> <asp: textbox id = TextBox1 runat = "server" Height = "20" Width = "300" text = ". // text () "OnKey_Up =" On_KeyUp "> </asp: textbox> </td>
<Td> <asp: button type = submit OnClick = "Query_Click" runat = "server" Height = "20" Width = "91" text = "Query"> </asp: button> </td>
</Tr>
</Table>

</Br>
<Table>
<Tr> <td> Output from Query </td> <td> XML Data </td> <tr>
<Tr> <td> Query Display: <asp: dropdownlist runat = "server">
<Asp: listitem> Descriptive </asp: listitem>
<Asp: listitem> XML </asp: listitem>
</Asp: dropdownlist>
</Td> <tr>
<Tr>
& Lt; td width = "50%" valign = "top" align = "left" & gt; & lt; asp: textbox id = TextBox2 runat = "server" Height = "400" Width = "350" TextMode = "MultiLine" Rows = "10"> </asp: textbox> </td>
<Td width = "50%" valign = "top" align = "left"> <asp: xml id = "MyXml" transformsource = "test. xsl "runat = server/> </asp: xml> </td>
</Tr>
</Table>
</Div>

<TD> <asp: textbox id = textbox3 runat = "server" Height = "1" width = "5" textmode = "multiline" rows = "110"> </ASP: textbox> </TD>

</Form>

</Body>
</Html>

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.