日期:2014-05-17  浏览次数:20539 次

asp.net 循环添加xml节点
功能是为了实现调用快递接口,对快递运单的批量查询

功能初始页面

输入数据后


现在遇到的问题就是怎么动态添加xml节点,想实现的结果是
XML code

 <BatchQueryRequest>
  <logisticProviderID>xxx</logisticProviderID> 
  <clientID>xxx</clientID> 
  <orders> 
    <order>
        <mailNo>1111111111</mailNo>
    </order>
    <order>
        <mailNo>2222222222</mailNo>
    </order>
   </orders> 
 </BatchQueryRequest>



后台代码
C# code

            string orderNo = this.IndexwaybillNo.Value.Trim();
            string clientId = "xxx"; 
            string logisticProviderID = "xxx"; 
            string[] rs = orderNo.Trim().Split('/');
            string xml = "<BatchQueryRequest>" +
                      "<logisticProviderID>" + logisticProviderID + "</logisticProviderID>" +
                      "<clientID>" + clientId + "</clientID>" +
                      "<orders>" +
                      "</orders>" +
                  "</BatchQueryRequest>";
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(xml);
            XmlNode xmlNode = xmlDoc.SelectSingleNode("BatchQueryRequest/orders");         
            foreach (string s in rs)
            {
                if (s.Trim().Length!= 10)
                {
                    this.lblTime.Text = "";
                    Common.Alert("运单号输入不正确,请输入10位正确运单号");                  
                }
                else if (!op.StrIsInt(s))
                {
                    this.lblTime.Text = "";
                    Common.Alert("运单号格式不正确,请输入10位正确运单号"); 
                }
                else
                {
                    orderNo = s;                   
                    if (xmlNode != null)
                    {
                        XmlElement newElement = xmlDoc.CreateElement("order");//添加order节点
                        XmlElement etitle = xmlDoc.CreateElement("mailNo"); //添加mailNo节点
                        etitle.InnerXml = orderNo;
                        newElement.AppendChild(etitle);                      
                        xmlNode.AppendChild(newElement);
                    }
                    xmlDoc.LoadXml(xml);
                }
                
            }



刚开始接触xml,节点就是添加不上。求高人指点!!!!!!!

------解决方案--------------------
直接写xml=xmlDoc.OuterXml; 就可以了。 xmlDoc.LoadXml(xml);这个删掉