当前位置:编程学习 > C#/ASP.NET >>

急!xml文件解析的问题!大家帮帮忙!

怎么将xml文件里的信息解析出来,并输出到asp.net2.0页面上。谢谢大家。 --------------------编程问答-------------------- 用datatable直接load那个需要解析的xml然后把gridview的数据源绑定为这个datatable..... --------------------编程问答-------------------- 不是这个意思,我是想把信息提取出来。例如:
< CustomerInfo I D = " 0 0 1 " CustomerJobName="Book"CustomerOrderID="ICSsample">
<Contact ContactTypes="Administrator">
<Address City="xi'an" Country="china"PostalCode="710048"Street="5#"/>
<Company OrganizationName="xaut"/>
<Person FamilyName="xue"FirstName="lian"/>
<ComChannel ChannelType="Phone"Locator="82134000"/>
<Comment>Custromer Comment on TU Xi'an</Comment>
</Contact>
</CustomerInfo>
想把I D = " 0 0 1 “提取出来,在页面显示ID=001,且001是在textbox中,也可以修改。 --------------------编程问答-------------------- 方法很多..可以自己写个解析的过程...
var GetXmlData=function (xmlObj,tagName){var node=xmlObj.getElementsByTagName(tagName);if(node!=null&&node[0]!=null&&node[0].firstChild!=null){return node[0].firstChild.data;};return "";};

传入一个XML  类似..
var MapAnnounceExt=function(xmlDoc)
{     
    var obj=new Object();            
    var k=0;    
    var rootObj = xmlDoc.responseXML.documentElement;
    if(rootObj!=null)
    {          
obj.SiteCode= GetXmlData(rootObj,"SiteCode");
obj.SourceID= GetXmlData(rootObj,"SourceID");
obj.SourceType= GetXmlData(rootObj,"SourceType");
obj.SourceFlag= GetXmlData(rootObj,"SourceFlag");
obj.FromSiteCode= GetXmlData(rootObj,"FromSiteCode");
obj.FromSourceID= GetXmlData(rootObj,"FromSourceID");
obj.CreateDate= GetXmlData(rootObj,"CreateDate");
obj.ShareStatus= GetXmlData(rootObj,"ShareStatus");
obj.FromID= GetXmlData(rootObj,"FromID");
obj.Title= GetXmlData(rootObj,"Title");
obj.Logo= GetXmlData(rootObj,"Logo");
obj.Content= GetXmlData(rootObj,"Content");
obj.Comment= GetXmlData(rootObj,"Comment");
obj.ViewCount= GetXmlData(rootObj,"ViewCount");
obj.StartDate= GetXmlData(rootObj,"StartDate");
obj.EndDate= GetXmlData(rootObj,"EndDate");
obj.Status= GetXmlData(rootObj,"Status");
obj.ModUser= GetXmlData(rootObj,"ModUser");
obj.ModDate= GetXmlData(rootObj,"ModDate");
obj.CreateUser= GetXmlData(rootObj,"CreateUser");
obj.CategID= GetXmlData(rootObj,"CategID");
    }
    return obj;
};


要是列表类型的就这样:
var MapAnnounceExts=function(xmlDoc)
{     
    var rs=new Array();
    var k=0;
    var rootObj = xmlDoc.responseXML.documentElement;
    if(rootObj!=null)
    {
var list=rootObj.getElementsByTagName("AnnounceExt"); 
 if(list!=null && list.length>0)
         {
            for(var i=0;i<list.length;i++)
            {
                 if(list[i]!=null)
                 {  
                    var obj=new Object();  
obj.SiteCode= GetXmlData(list[i],"SiteCode");
obj.SourceID= GetXmlData(list[i],"SourceID");
obj.SourceType= GetXmlData(list[i],"SourceType");
obj.SourceFlag= GetXmlData(list[i],"SourceFlag");
obj.FromSiteCode= GetXmlData(list[i],"FromSiteCode");
obj.FromSourceID= GetXmlData(list[i],"FromSourceID");
obj.CreateDate= GetXmlData(list[i],"CreateDate");
obj.ShareStatus= GetXmlData(list[i],"ShareStatus");
obj.FromID= GetXmlData(list[i],"FromID");
obj.Title= GetXmlData(list[i],"Title");
obj.Logo= GetXmlData(list[i],"Logo");
obj.Content= GetXmlData(list[i],"Content");
obj.Comment= GetXmlData(list[i],"Comment");
obj.ViewCount= GetXmlData(list[i],"ViewCount");
obj.StartDate= GetXmlData(list[i],"StartDate");
obj.EndDate= GetXmlData(list[i],"EndDate");
obj.Status= GetXmlData(list[i],"Status");
obj.ModUser= GetXmlData(list[i],"ModUser");
obj.ModDate= GetXmlData(list[i],"ModDate");
obj.CreateUser= GetXmlData(list[i],"CreateUser");
obj.CategID= GetXmlData(list[i],"CategID");
rs[k++]=obj;
   }   
            }
        }
    }
    return rs;
}; --------------------编程问答-------------------- xml+xslt 不用写代码,自动解析 --------------------编程问答-------------------- 必须会写xslt,才能全面的解析xml文件,


不用找什么捷径,xslt是最实用的方法。而且xslt也很容易。


在vs2005和以上版本中有asp:xml控件,很好的展示由xslt解析的内容。
补充:.NET技术 ,  ASP.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,