<?xml version="1.0" encoding="GBK"?>
-<data version="1.0"> -<trans_main> <trans_type>2</trans_type> <s_ent_name>XX公司</s_ent_name> <b_ent_name>XX公司</b_ent_name> <b_ent_type>0</b_ent_type> <b_licence_no/> <b_city_id/> <b_city_name/> <b_boro_id/> <b_boro_name/> <trans_date>2012-02-05</trans_date> <create_date>2012-02-10</create_date> <report_user>谷正艳</report_user> <user_link>15849772556</user_link> <if_network>0</if_network> </trans_main> 
-<trans_detail> <trans_type>2</trans_type> <pass_num/> <goods_name>开塞露</goods_name> <drug_name/> <goods_manu/> <drug_ename/> <standard>20ml</standard> <form>中成药外用</form> <lot_num>110301.</lot_num> <uom>盒</uom> <trans_amount>40</trans_amount> <prices/> <trans_num/> <report_amount/> <validate_date>2013-02-28 00:00:01</validate_date> </trans_detail> 
-<trans_detail> <trans_type>2</trans_type> <pass_num>国药准字(2002)</pass_num> <goods_name>妇炎洁洗液-180</goods_name> <drug_name/> <goods_manu/> <drug_ename/> <standard>180ml</standard> <form>中成药外用</form> <lot_num>110402</lot_num> <uom>并</uom> <trans_amount>10</trans_amount> <prices/> <trans_num/> <report_amount/> <validate_date>2013-03-31</validate_date> </trans_detail>
</data>[code=C#][/code]XML格式是这样的我怎么读取到数据库。

解决方案 »

  1.   

    http://www.cnblogs.com/XmNotes/archive/2010/08/12/1796162.html
      

  2.   

    你要读 写xml 吗? 是的话,看看这个贴子,顶望对你有助
      

  3.   

    楼上你让我看的那个我也会做。但是我现在想做的是一个<trans_main>包含多个<trans_detail>。我想读取的数据是XX公司向XX公司买的是什么东西,而且是每个详细的里面要包括main中的名称。
      

  4.   

    如果xml 这块,下面这个你看看,有xml 读 ,改,写的代码!慢慢看吧。希望对你有用。 
    //======================== 下面是添加一个节点 =========================
    XmlDocument xmlDoc = new XmlDocument();
    // 主要代码:
    xmlDoc.Load(Application.StartupPath + "\\data.xml");
    //找到要添加处
    XmlNode root = xmlDoc.SelectSingleNode("Employees"); //查找<Employees>
    //下面是操作
    XmlElement xe1 = xmlDoc.CreateElement("Node"); //创建一个<Node>节点
    {
    //设置属性
    xe1.SetAttribute("genre", "张三");
    xe1.SetAttribute("ISBN", "1-1111-1"); //设置该节点ISBN属性 XmlElement xesub1 = xmlDoc.CreateElement("title");
    xesub1.InnerText = "C#入门帮助"; //设置文本节点
    xe1.AppendChild(xesub1); //添加到<Node>节点中
    XmlElement xesub2 = xmlDoc.CreateElement("author");
    xesub2.InnerText = "高手";
    xe1.AppendChild(xesub2);
    XmlElement xesub3 = xmlDoc.CreateElement("price");
    xesub3.InnerText = "158.3";
    xe1.AppendChild(xesub3);
    }
    root.AppendChild(xe1);//添加到<Employees>节点中
    xmlDoc.Save(Application.StartupPath + "\\data.xml");//============== 下面结果 =============================================
    <?xml version="1.0" encoding="gb2312" standalone="yes"?>
    <Employees>
      <Node genre="李赞红" ISBN="2-3631-4">
        <title>CS从入门到精通</title>
        <author>候捷</author>
        <price>58.3</price>
      </Node>
      <Node genre="李赞红" ISBN="2-3631-4">
        <title>CS从入门到精通</title>
        <author>候捷</author>
        <price>58.3</price>
      </Node>
    </Employees>
    变成如下:
    <?xml version="1.0" encoding="gb2312" standalone="yes"?>
    <Employees>
      <Node genre="李赞红" ISBN="2-3631-4">
        <title>CS从入门到精通</title>
        <author>候捷</author>
        <price>58.3</price>
      </Node>
      <Node genre="李赞红" ISBN="2-3631-4">
        <title>CS从入门到精通</title>
        <author>候捷</author>
        <price>58.3</price>
      </Node>
      <Node genre="张三" ISBN="1-1111-1">
        <title>C#入门帮助</title>
        <author>高手</author>
        <price>158.3</price>
      </Node>
    </Employees>
      

  5.   

    要读取,  先找到节点,如下读取 XmlNode node = ;//找到节点!
    if (node != null)
    {
    string k1 = node.Value;    //null
    string k2 = node.InnerText;//Data Source=yf; user id=ctm_dbo;password=123
    string k3 = node.InnerXml;//Data Source=yf; user id=ctm_dbo;password=123
    node = null;
    }
      

  6.   

    下面是找到节点:XmlNode root = xmldoc.SelectSingleNode("Employees");//查找<Employees>//取得父节点
    XmlElement perentNode = xel.ParentNode; //取得子节点 集合 
    XmlNodeList nodeList = xmlDoc.SelectSingleNode("Employees").ChildNodes;
    //可用foreach 来遍历,
    foreach(NodeList nl in nodeList )
    {
      // nl //这是每个子节点
       //设属性
       nl .SetAttribute("genre", "李赞红");//设置该节点genre属性
    //取属性
       if (nl .GetAttribute("genre") == "张三"){}
    //设innerHtml
       nl.InnerText = "CS从入门到精通";
    //删除genre属性
       xe.RemoveAttribute("genre");
    }
      

  7.   

    用XmlDocument解析Xml,然后你就cnblogs/google查XmlDocument解析就搞定了