Using XML in Dotnet

Source: Internet
Author: User
Tags button type dotnet empty query sort tostring xsl
XML Oh, interesting, heard a joke, said an old man in Germany, is a deaf, in the toilet, suddenly Soviet warplanes air raids, although the house collapsed, but the whole family did not matter, the family to pull out the toilet wall, found the old man there, said: "I pull a flush toilet rope, the house fell."
hahaha, I also like this, just I one of this post, chinaasp is over, thought it was my thing, it is the Soviet plane to fry, hehe.
If you like to play with XML, then follow me, but before you do, please take a deep breath as I introduce you to a long code that reveals a Web Form control that Microsoft hides in the ASP.net architecture, that is, <asp:xml runat=server/> I only give code, do not give explanations, everyone to study after class.
In addition, because it is beta1, you cannot use &LT;XSL:SORT&GT in the XSLT you use in this control, and, of course, you cannot use that order-by, because it supports the XSL space with "1999" instead of the original one.
In addition, the answer I get from Microsoft is in Beta2, it will support <xsl:sort&gt, and by then, my brother will all turn to xml+xsl, now for the source code confidentiality problem to get a very headache.
Take a look at 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 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");
}

DOM *********************************
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");
}
}
}


Here's 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
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= "width=" "text=".//text () "onkey_up=" On_KeyUp " ></asp:textbox></td>
<td><asp:button type=submit runat= "Server" height= "width=" "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" ><asp:textbox id=textbox2 "server" runat= "350" Textmode= "MultiLine" rows= "ten" ></asp:textbox></td>
&LT;TD width= "50%" valign= "Top" align= "left" ><asp:xml "id=" myXML "transformsource=" test.xsl </asp:xml></td>
</tr>
</table>
</div>

<td><asp:textbox id=textbox3 runat= "Server" height= "1" width= "5" textmode= "MultiLine" rows= "" "></" 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.