从xml中提取节点数据

本文关键字:节点 数据 提取 xml | 更新日期: 2023-09-27 18:12:37

我有一个这样的xml:

<People>
  <PersonID>5</PersonID>
  <PersonID>7</PersonID>
  <PersonID>9</PersonID>
</People>

我想创建一个字符串与所有的id,像这样:"5,7,9"。我知道用一个简单的循环是可能的,但由于这段代码将每秒调用100次,我想使它尽可能快的方式。

我使用的是c# framework 4.0

我的当前代码:

XmlDocument doc = new XmlDocument();
XmlNodeList nodeList;
StringBuilder strXml = new StringBuilder();
doc.LoadXml(sXmlQuery);
nodeList = doc.SelectNodes("//PersonID");
if (nodeList != null)
{
    foreach (XmlNode node in nodeList)
    {
        strXml.Append(node.InnerText.Trim());
        strXml.Append(",");
    }
}

从xml中提取节点数据

您可以使用LINQ到XML与字符串。加入:

XDocument xmlDoc = XDocument.Parse(@"<People>
                                      <PersonID>5</PersonID>
                                      <PersonID>7</PersonID>
                                      <PersonID>9</PersonID>
                                    </People>");
var val = xmlDoc.Descendants("People")
                 .SelectMany(r => r.Elements("PersonID"))
                 .Select(r => r.Value);
string str = string.Join(",", val);

str将是str = "5,7,9"

我不知道它对你来说是否足够快,但我会试一试

string s = String.Join("," , xDoc.Descendants("PersonID").Select(p => (string)p));

尝试以下代码:

 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;
}

访问这些代码:

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

根据您的要求自定义,希望这将为您工作