Create an online communication record, realize the online address book display, add, delete

Source: Internet
Author: User
Tags add define object string version variable tostring xmlns
Show | online

Project Description: To create an online communication record, the realization of online address Book display, add, delete

Project Analysis:

I. Data files

The required XML file (Txl.xml) is as follows:

<?xml version= "1.0" encoding= "UTF-8"?>

<?xml-stylesheet type= "text/xsl" href= "txl.xsl"?>

<txl>

<user>

<pic>1.bmp</pic>

<name>ittalk</name>

<tel>51872033</tel>

<qqmsn>7987931</qqmsn>

<mobil>13667181380</mobil>

<work> North Jade Bird </work>

<address> Beida Jade Bird Wuhan Derby </address>

<email>ninesunnine@sina.com</email>

<otherMsg> No </otherMsg>

</user>

<user>

<pic>2.bmp</pic>

<name>ittalk</name>

<tel>51872033</tel>

<qqmsn>7987931</qqmsn>

<mobil>13667181380</mobil>

<work> North Jade Bird </work>

<address> Beida Jade Bird Wuhan Derby </address>

<email>ninesunnine@sina.com</email>

<otherMsg> No </otherMsg>

</user>

</txl>

This XML file corresponds to a stylesheet txl.xsl

<?xml version= "1.0" encoding= "UTF-8"?>

<xsl:stylesheet version= "1.0" xmlns:xsl= http://www.w3.org/1999/XSL/Transform "xmlns:fo=" http://www.w3.org/ 1999/xsl/format ">

<xsl:template match= "/" >

<table align= "center" width= "90%" border= "1" >

<tbody>

<tr bgcolor= "#336699" >

<th> Pictures </th>

<th> name </th>

<th> Tel </th>

<th>qq/Msn</th>

<th> Mobile </th>

<th> Work Unit </th>

<th> Mailing Address </th>

<th>Email</th>

<th> Other Information </th>

</tr>

<xsl:apply-templates select= "/txl/user"/>

</tbody>

</table>

</xsl:template>

<xsl:template match= "User" >

<tr>

<td><xsl:variable name= "x" ><xsl:value-of select= "pic"/></xsl:variable></img></td>

<td><xsl:value-of select= "Name"/></td>

<td><xsl:value-of select= "Tel"/></td>

<td><xsl:value-of select= "Qqmsn"/></td>

<td><xsl:value-of select= "Mobil"/></td>

<td><xsl:value-of select= "Work"/></td>

<td><xsl:value-of select= "Address"/></td>

<td><xsl:value-of select= "Email"/></td>

<td><xsl:value-of select= "Othermsg"/></td>

</tr>

</xsl:template>

</xsl:stylesheet>

Second, display XML file

To display an XML file by using the XSL Auto style:

1. Define a System.IO.StringWriter object first

2. Create an XML Document object and download the XML file at home

3. Create a XslTransform object and load the XSL file

4. Call conversion function (first parameter XML Document object, second parameter null, third parameter StringWriter object, fourth parameter null)

5, call the Response.writer method to display the data in the StringWriter object.

First define a System.IO.StringWriter object

System.IO.StringWriter sw=new StringWriter ();

Create a document FOR XML

System.Xml.XmlDocument doc=new XmlDocument ();

Loading an XML document

Doc. Load (Server.MapPath ("Txl.xml"));

Create a System.Xml.Xsl.XslTransform object

System.Xml.Xsl.XslTransform xst=new XslTransform ();

Load the corresponding style sheet for the XML file txl.xsl

Xst. Load (Server.MapPath ("txl.xsl"));

Use XST to convert Doc's display

Xst. Transform (Doc,null,sw,null);

Response.Write (SW. ToString ());

Third, add users to the communications record

Related knowledge:

1, the XmlDocument object is the root of all dom trees, this root has at least one child node, that is, the root element, which is represented by DocumentElement, can also have a root element, that is, DocumentType represents the DTD type.

2, some properties of the XmlDocument object

A) HasChildNodes

b) childnodes

c) parentnode

d) The concatenation value of the InnerXml attribute node and its child nodes.

e) OuterXml A token representing the node and its child nodes

How to update a file

1, first create an XML Document object, while loading the corresponding XML file

2. Get the root node of XML Document object root

3, create an element, and initialize the element.

4. Place the element in the corresponding position.

5, create the corresponding child elements of the elements in 4, and add the corresponding elements below the elements in 4.

6, remember to save the modified XML file, this time you need to call the XmlDocument object's saving method.

First create an XML Document object and load the corresponding XML file

System.Xml.XmlDocument doc=new XmlDocument ();

Load Txl.xml File

Doc. Load (Server.MapPath ("Txl.xml"));

Locate the root node of the file

XmlNode Root=doc. DocumentElement;

Create an element user in doc

XmlElement User=doc. createelement ("user");

Add the user element to the tail of the root

Root. AppendChild (user);

Create an element name in doc

XmlElement Name2=doc. createelement ("name");

Set the text content of the name element

Name2. Innertext=name1. Text;

XmlElement Tel1=doc. CreateElement ("tel");

Tel1. Innertext=tel. Text;

XmlElement Qqmsn1=doc. CreateElement ("Qqmsn");

QQMSN1. Innertext=qqmsn. Text;

XmlElement Mobil1=doc. CreateElement ("Mobil");

Mobil1. Innertext=mobil. Text;

XmlElement Work1=doc. CreateElement ("work");

Work1. Innertext=work. Text;

XmlElement Address1=doc. createelement ("Address");

Address1. Innertext=address. Text;

XmlElement Email1=doc. createelement ("email");

Email1. Innertext=email. Text;

XmlElement Othermsg1=doc. CreateElement ("othermsg");

Othermsg1.innertext=othermsg.text;

Adds the child elements of the created user element to the node within the user element.

User. AppendChild (name2);

User. AppendChild (TEL1);

User. AppendChild (QQMSN1);

User. AppendChild (MOBIL1);

User. AppendChild (WORK1);

User. AppendChild (ADDRESS1);

User. AppendChild (EMAIL1);

User. AppendChild (OTHERMSG1);

Save changes to Txl.xml file in file

Doc. Save (Server.MapPath ("Txl.xml"));

Find a user in the communication record

Basic knowledge: You need to read the XML data and compare it.

1. To read XML, you can use the XmlReader class, but the class is an abstract class that reads the XmlTextReader class of a text-based XML file and implements the XmlReader class.

2. You should use the XmlTextReader class when you have to read a portion of a document into memory through the DOM.

3, first create an instance of the XmlTextReader class, and call the Read method to read the file.

A The parameter can be passed as an XML file when the instance is created

b) Call the Read () method to read its node one at a point until the end of the file is reached.

Declare and create an object for the XmlTextReader class XTR

XmlTextReader xtr=new XmlTextReader (Server.MapPath ("Txl.xml"));

String wsx= "";

BOOL Flag=false;

String Showmsg= "";

String Tmpnodename= "";

Using the Read () method of the Xtr object to iterate through the node of the file

while (XTR. Read ())

{

When you find that person, the type of the node is the element at the same time the node's name is User exit loop

if (Flag==true && xtr. Nodetype==system.xml.xmlnodetype.element && XTR. Localname.equals ("user"))

Break

When the node type is an element, the node name of the element is logged

if (XTR. Nodetype==system.xml.xmlnodetype.element)

Tmpnodename=xtr. Localname.tostring ();

When the specified person is not found, determine if the XTR is not a text type

if (Flag==false)

if (XTR. Nodetype==system.xml.xmlnodetype.text)//If the text type is so

if (XTR. Value==username.text////When the value for the text type is the same as the person to find

{

flag=true;//Setup found the logo for the person you're looking for.

}

If you've found this person at the same time XTR is currently a text type so

if (Flag==true && xtr. Nodetype==system.xml.xmlnodetype.text)

ShowMsg = "" +tmpnodename+ ":" +xtr. Value;

}

Close XTR

Xtr. Close ();

Show details of the person found

Response.Write (SHOWMSG);

V: Use Xmlpath to delete the specified node

First define a Document object

XmlDocument doc=new XmlDocument ();

Doc. Load (Server.MapPath ("Txl.xml"));

XmlNode Xn=doc. selectSingleNode ("/txl/user[name=\" "+ Username.text +" \ "]);

Try

{

XmlNode Parentxn=xn. ParentNode;

Parentxn. RemoveChild (xn);

}

catch (Exception ex)

{

Response.Write (ex. Message.tostring ());

}

Doc. Save (Server.MapPath ("Txl.xml"));

VI: Use XMLPATH to update the specified node

XmlDocument doc=new XmlDocument ();

Doc. Load (Server.MapPath ("Txl.xml"));

Use the XmlDocument SelectNodes method to find all user names that are compliant with the specified username

XmlNode Xn=doc. selectSingleNode ("/txl/user[name=\" "+ Username.text +" \ "]);

Try

{

Traversing child nodes

for (int i=0;i<xn. childnodes.count;i++)

{

Assigning the contents of a corresponding child node

Xn. Childnodes[i]. innertext= (i). ToString ();

}

Doc. Save (Server.MapPath ("Txl.xml"));

}

catch (Exception ex)

{

Response.Write (ex. message);

}

Vii. conversion of data into dataset

System.Data.DataSet ds=new DataSet ();

Ds. READXML (Server.MapPath ("Txl.xml"));

Datagrid1.datasource=ds;

Datagrid1.databind ();

Viii. writing DataSet data to XML

SqlConnection con=new SqlConnection ("server=.; uid=sa;pwd=;d atabase=pubs ");

SqlDataAdapter Da=new SqlDataAdapter ("select * from authors", con);

System.Data.DataSet ds=new DataSet ();

Da. Fill (DS);

Writes data from a dataset to XML

Ds. WriteXml (Server.MapPath ("Authors.xml"));

Datagrid1.datasource=ds;

Datagrid1.databind ();










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.