xml

<ufinterface roottag="accept" billtype="" docid="" receiver="" sender="" proc="" codeexchanged="" exportneedexch="" version="2.0">
<accept>
<header>  <vouchtype /> 
  <vouchcode /> 
  <vouchdate /> 
  <period /> 
  <customercode /> 
  <departmentcode /> 
    <sitemcode /> 
  <citemname /> 
  <oppositebankname /> 
  <bankname /> 
  <bankaccount /> 
  <define1 /> 
  <define2 /> 
  <define3 /> 
  <define4 /> 
  <define5 /> 
  <define6 /> 
  <define7 /> 
  <define8 /> 
  <define9 /> 
  <define10 /> 
  <dealmode /> 
  </header> <body><entry>  <mainid /> 
  <type /> 
  <customercode /> 
  <originalamount /> 
  <amount /> 
  <itemcode /> 
  <projectclass /> 
  <project /> 
  <departmentcode /> 
  <personcode /> 
  <orderid /> 
  <itemname /> 
  <ccontype /> 
  <cconid /> 
  <iamt_s /> 
  <iramt_s /> 
  </entry>  
</body>  </accept>
  </ufinterface>head为主表,body 为子表(可能有多条数据),我的xml模板如上,怎样动态往body插入数据。。

解决方案 »

  1.   

    xmlNode = mXMLOrderInfo.SelectSingleNode("//body//entry");
                XmlNode xmlNodebody = mXMLOrderInfo.SelectSingleNode("//body");
                XmlNode xmlNodeCopy = xmlNode.Clone();            for (int i = 0; i < j; i++)
                {
                    if (i == 0)
                    {
                        CreateXmlNode(xmlNode, dt, ds, i);
                    }
                    else
                    {
                        //XmlNode xmlNodeNew = xmlNodeCopy.Clone();                    CreateXmlNode(xmlNodeCopy, dt, ds, i);
                        xmlNodebody.AppendChild(xmlNodeCopy);                }
                }
     //创建节点
            public static void CreateXmlNode(XmlNode xmlNode, DataTable dt, DataTable mHeadTable, int j)
            {
                string TabFieldName;
                DataRow[] rows = null;
                rows = dt.Select();
                // xmlNode.ParentNode;
                foreach (var item in rows)
                {
                    string xmlNodeName = item["XmlField"].ToString();
                    TabFieldName = item["TableField"].ToString();
                    // XmlNode q = xmlNode.ParentNode;
                    XmlNode tempXmlNode = null;
                    if (j < 1)
                    {
                        tempXmlNode = xmlNode.SelectSingleNode("//body//entry//" + xmlNodeName);
                    } if (j >= 1)
                    {
                        tempXmlNode = xmlNode.SelectSingleNode("//entry//" + xmlNodeName);
                    }                if (tempXmlNode != null)
                    {
                        tempXmlNode.InnerText = mHeadTable.Rows[j][TabFieldName].ToString();
                    }            }
            }
    这样写的代码不行,如果子表有多条记录只能插入第一条。。求高手指教。。
      

  2.   

    就没人懂吗?求高手指教啊。。
    xmlNode.SelectSingleNode(xmlNodeName)//只找第一个的节点,第二个节点怎样查询?
    我用以上的方法来查找xml节点是否存在。如果我的body部分有两条entry,哪我应该怎样区分是第一个的还是第二个的。。
      

  3.   

    首先构建 entry 节点
    xmlNode = mXMLOrderInfo.SelectSingleNode("ufinterface/accept/body");
    xmlNode.ChildNodes.Add(entry 节点)
      

  4.   

    XmlNodeList nodes=xmlDoc.SelectNodes(xpath表达式);
    要取任意一个都可以。
      

  5.   


    XmlNodeList nodeList;
    XmlNode root = doc.DocumentElement;
    nodeList=root.SelectNodes(xmlNodeName);