将字符串转换成xml并取得对应的值_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > 将字符串转换成xml并取得对应的值

将字符串转换成xml并取得对应的值

 2013/8/5 19:08:34  hellocjr  博客园  我要评论(0)
  • 摘要:如数据库中有一个字段保存了xml格式的一串字符串:<?xmlversion="1.0"encoding="utf-16"?><ArrayOfPassengerInfoForXmlxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="http://www.w3.org/2001/XMLSchema"><PassengerInfoForXml><Person>1<
  • 标签:字符串 XML

如数据库中有一个字段保存了xml格式的一串字符串:

<?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>张三</Name>
    <InsureNumber>2</InsureNumber>
  </PassengerInfoForXml>
  <PassengerInfoForXml>
    <Person>1</Person>
    <Name>李四</Name>
    <InsureNumber>1</InsureNumber>
  </PassengerInfoForXml>
</ArrayOfPassengerInfoForXml>

 

方法一:js

//字符串转化为xml
function 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为上面的xml格式的字符串
 var s = toXmlDom(strXML); 
 $(s).find("PassengerInfoForXml").each( //得到每一个签
     function (id, item) {
            //获取标签的内容
            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);
         }
    );

}

 

方法二:asp.net 后台

1) 建一个类PassengerInfoForXml.cs,对应每个标签的字段

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) 获取每个标签的值

  protected List<PassengerInfoForXml> passengerlist = null;
        string strxml = "";  //上面的xml格式字符串

        protected void Button1_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>");
            }
        }
        //转换成对象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);
            }
        }

 

 

 

 

发表评论
用户名: 匿名