Extract nodes data from xml

后端 未结 3 1598
无人共我
无人共我 2021-01-14 22:57

I have an xml like this:


  5
  7
  9
         


        
3条回答
  •  囚心锁ツ
    2021-01-14 23:08

    Try the following code:

     public DataTable GetColumnsFromXML(String XMLPath, String TableName)
    {
        DataTable dtForColumns = DatatableforColumns();
        XmlDataDocument xmldoc = new XmlDataDocument();
        xmldoc.Load(XMLPath);
        XmlElement root = xmldoc.DocumentElement;
        XmlNodeList tablenodes = root.SelectNodes("Table");
    
        if (tablenodes != null)
    
            foreach (XmlNode nodes in tablenodes)
            {
                if (!nodes.HasChildNodes) continue;
                if (nodes.Attributes == null) continue;
                //TableName = nodes.Attributes[0].Value;
    
                if (nodes.Attributes[0].Value == TableName)
                {
                    String PrimaryKey = nodes.Attributes[1].Value;
                    var nodesdisplayname = nodes.SelectNodes("Column/DisplayColumn");
                    var nodesorignalvalue = nodes.SelectNodes("Column/OrignalColumn");
    
                    if (nodesdisplayname != null && nodesorignalvalue != null)
                    {
                        for (int i = 0; i <= nodesdisplayname.Count - 1; i++)
                        {
                            var xmlDisplayNode = nodesdisplayname.Item(i);
                            var xmlOrignalNode = nodesorignalvalue.Item(i);
    
    
                            if (xmlDisplayNode != null && xmlOrignalNode != null)
                            {
    
                                DataRow dr;
                                dr = dtForColumns.NewRow();
                                dr["DisplayColumn"] = xmlDisplayNode.InnerText;
                                dr["OrignalColumn"] = xmlOrignalNode.InnerText;
                                dr["PrimaryKey"] = PrimaryKey;
                                dtForColumns.Rows.Add(dr);
                            }
                        }
                    }
                }
            }
        return dtForColumns;
    }
    private DataTable DatatableforColumns()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("DisplayColumn", typeof(String));
        dt.Columns.Add("OrignalColumn", typeof(String));
        dt.Columns.Add("PrimaryKey", typeof(String));
        return dt;
    }
    

    To Access these here is the code:

    DataTable dtColumns = new DataTable();        
    dtColumns = objXML.GetColumnsFromXML(Server.MapPath("~/XMLFile.xml"), TableName);
    

    Customize it according to your requirement, hope this will work for you.

提交回复
热议问题