C#读取XML文件,并把结果按照JSON格式返回。

        public ActionResult GetInventoryTypeList(string CustomerID, string InventoryID)
        { 
            List<string> InventoryTypeList = new List<string>();
            List<string> InventoryIDList = new List<string>();
            string CustomerID_ = "";
            string InventoryType_ = "";
            string z = "";
            string str = "";
            XmlDocument doc = new XmlDocument();
           string BaseDir = HttpRuntime.AppDomainAppPath.ToString();
            doc.Load(BaseDir+@"/GoodsStatus.xml");
            // 得到根节点bookstore
            XmlNode xn = doc.SelectSingleNode("GoodsStatus"); 
  
            // 得到根节点的所有子节点
            XmlNodeList xnl = xn.ChildNodes;

            foreach (XmlNode x in xnl)
            {
                XmlElement xe = (XmlElement)x;
                CustomerID_      = xe.GetAttribute("CustomerID").ToString();
                InventoryType_   = xe.GetAttribute("InventoryType").ToString();
                InventoryIDList  = xe.GetAttribute("InventoryType").ToString().Split(',').ToList<string>(); 

if (CustomerID_ == CustomerID && InventoryIDList.Contains(InventoryID)) { Common.WriteLog(" compare OK " ); XmlNodeList xnl0 = xe.ChildNodes; foreach (XmlNode xx in xnl0) { XmlElement xxe = (XmlElement)xx; str = string.Format("['name':'{0}','value':'{1}']", xxe.GetAttribute("name").ToString() , xxe.GetAttribute("value").ToString()); InventoryTypeList.Add(str.Replace("[","{").Replace("]", "}").Replace("'", "\"")); Common.WriteLog(str); } } } return Json(InventoryTypeList); }
<?xml version="1.0" encoding="utf-8" ?>
<!---  
 --> 

<GoodsStatus>
  <list CustomerID="YH10121701" InventoryType="8010,10215,10121,10260"> 
    <option name="普通好机" value="8010">8010</option> 
    <option name="BLK1"     value="10110">10110</option>  
  </list> 
 
</GoodsStatus>
 

 

posted on 2017-05-12 17:27  易宝1  阅读(383)  评论(0)    收藏  举报