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

asp.net 循环添加节点,求帮助???????
C# code

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


我想给orders节点下循环添加多个order节点
实现以下效果
XML code

 <BatchQueryRequest>
  <logisticProviderID>cbc</logisticProviderID> 
  <clientID>abc</clientID> 
  <orders> 
    <order>
        <mailNo>1000000001</mailNo>
    </order>
    <order>
        <mailNo>1000000002</mailNo>
    </order>
        <order>
        <mailNo>1000000003</mailNo>
    </order>
  </orders> 
 </BatchQueryRequest>


到了xmlDoc.save(xml)就报错了。错误提示:路径中具有非法字符。

------解决方案--------------------
xmlDoc.save(xml)这里有问题,xmlDoc.save(filename)这里参数应该是带路径的文件名