Converts a string to xml and obtains the corresponding value.

Source: Internet
Author: User

Converts a string to xml and obtains the corresponding value.

For example, a field in the database stores a string in xml format:

<? Xml version = "1.0" encoding = "UTF-16"?>
<ArrayOfPassengerInfoForXml xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: xsd = "http://www.w3.org/2001/XMLSchema">
<PassengerInfoForXml>
<Person> 1 </Person>
<Name> Zhang San </Name>
<InsureNumber> 2 </InsureNumber>
</PassengerInfoForXml>
<PassengerInfoForXml>
<Person> 1 </Person>
<Name> Li Si </Name>
<InsureNumber> 1 </InsureNumber>
</PassengerInfoForXml>
</ArrayOfPassengerInfoForXml>

 

Method 1: js

// Convert the string to xmlfunction toXmlDom (source) {var xmlDoc = null; if (window. activeXObject) {var ARR_ACTIVEX = ["MSXML4.DOMDocument", "MSXML3.DOMDocument", "MSXML2.DOMDocument", "MSXML. DOMDocument "," Microsoft. xmlDom "]; var XmlDomflag = false; for (var I = 0; I <ARR_ACTIVEX.length &&! XmlDomflag; I ++) {try {var objXML = new ActiveXObject (ARR_ACTIVEX [I]); xmlDoc = objXML; XmlDomflag = true;} catch (e) {}} if (xmlDoc) {xmlDoc. async = false; xmlDoc. loadXML (source) ;}} else {var parser = new DOMParser (); var xmlDoc = parser. parseFromString (source, "text/xml");} return xmlDoc ;}

 

Function getXmlDom (source) {var strXML = ""; // strXML is the above xml Format String var s = toXmlDom (strXML); $ (s ). find ("PassengerInfoForXml "). each (// obtain each signature function (id, item) {// obtain the TAG content var Person =$ (item ). find ("Person "). eq (0 ). text (); var Name = $ (item ). find ("Name "). eq (0 ). text (); var InsureNumber = $ (item ). find ("InsureNumber "). eq (0 ). text (); alert (Name );});}

 

Method 2: asp.net background

1) create a class PassengerInfoForXml. cs that corresponds to the fields of each tag.

using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace WebApplication1{    public class PassengerInfoForXml    {        public Int32 Person { get; set; }        public String Name { get; set; }        public Int32 InsureNumber { get; set; }    }}

2) obtain the value of each tag

Protected List <PassengerInfoForXml> passengerlist = null; string strxml = ""; // The preceding xml Format string protected void button#click (object sender, EventArgs e) {passengerlist = ConvertToObject (strxml, typeof (List <PassengerInfoForXml>) as List <PassengerInfoForXml>; for (int I = 0; I <passengerlist. count; I ++) {var Person = passengerlist [I]. person; var Name = passengerlist [I]. name; var InsureNumber = passengerlist [I]. insureNumber; Response. write ("<script> alert ('" + Name + "') </script>");} // convert to the object PassengerInfoForXml public static object ConvertToObject (string xml, type objectType) {object obj2 = null; if (string. isNullOrEmpty (xml) {return obj2;} using (StringReader reader = new StringReader (xml) {XmlSerializer serializer = new XmlSerializer (objectType); return serializer. deserialize (reader );}}

 

 

 

 

Tag: xml, string

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.