【问题标题】:Read XML Document using C# or Java使用 C# 或 Java 读取 XML 文档
【发布时间】:2012-05-17 03:47:23
【问题描述】:

我想阅读下面的 XML 格式,只有一些属性是必需的,而不是全部。 例如:

<Parts>
-   <Part>
        <Section>3003512</Section> 
        <Mark>RP-103</Mark> 
        <Length>4950</Length> 
        - <Components>
                <Section>3003512</Section> 
                <Mark>RP-103</Mark> 
                <Length>4950</Length>
                <Remark>System Generated </Remark>
              <Components />
            <Remark>No Comments </Remark>
        </Part>

-   <Part>
        <Section>3003512</Section> 
        <Mark>RP-103</Mark> 
        <Length>4950</Length> 
        <Components />
        <Remark>No Comments </Remark>
     </Part>
</Parts>

我想阅读只读部分并以表格格式标记。我正在使用下面的代码来阅读这个但是它给出了错误表架构'组件'已经存在。

        DataTable dt = new DataTable();
        DataColumn dc = new DataColumn("Mark");
        DataColumn dc1 = new DataColumn("Sections ");
        dt.Columns.Add(dc); 
        dt.Columns.Add(dc1);
        DataSet dSet = new DataSet();


        if (File.Exists(xmlpath2))
        {

            XmlTextReader Reader1 = new XmlTextReader(xmlpath2);



            dSet.ReadXml(Reader1, XmlReadMode.Auto);


            for (int i = 0; i < dSet.Tables[0].Rows.Count; i++)
            {
                DataRow rows = dSet.Tables[0].Rows[i];
                DataRow myRow = dt.NewRow();
                myRow["Mark"] = rows["Mark"];
                myRow["Sections "] = rows["Sections "];

                dt.Rows.Add(myRow);
            }


            GridView1.DataSource = dt;
            GridView1.DataBind(); 

        }

【问题讨论】:

  • 有几个 XML 库可以帮助解决这个问题。尝试更多搜索,然后回来询问。
  • 下定决心——Java 或 C##,因为给出的答案会因选择而异。
  • 源代码中所有多余的“空白”行是怎么回事。它应该让代码运行得更快吗?
  • rows["Sections "] 与您的
    标记不匹配。

标签: c# java xml xml-parsing


【解决方案1】:

这是一个使用LINQ to XML的例子:

var ele = XElement.Parse(xml); // change to XElement.Load if loading from file

var result = ele.Descendants("Section")
                .Zip(ele.Descendants("Mark"), 
                    (s,m) => new {Section = s.Value, Mark = m.Value});

现在你可以创建你的DataTable:

var table = new DataTable();
var marks = new DataColumn("Mark");
var sections = new DataColumn("Sections");

table.Columns.Add(marks);
table.Columns.Add(sections);

foreach (var item in result)
{
    var row = table.NewRow();
    row["Mark"] = item.Mark;
    row["Sections"] = item.Section;
    table.Rows.Add(row);
}

这将产生:

Mark     Sections 
RP-103   3003512 
RP-103   3003512 
RP-103   3003512 

假设每个Section 后面跟着一个对应的Mark。它还需要System.Xml.LinqSystem.Linq

【讨论】:

  • 代码运行良好。谢谢。请让我知道如何在下面的代码中绑定 XML 文档中的三个值。 var result = ele.Descendants("Section") .Zip(ele.Descendants("Mark"), (s,m) => new {Section = s.Value, Mark = m.Value});
  • @kuldeepverma 我已经更新了答案以展示如何创建DataTable。这里没有更多帮助。
  • 谢谢,我已经这样做了。但我无法将更多列 .4950 绑定到数据表中。
【解决方案2】:

不久前正在做类似的事情: LINQ multiple columns

希望能让你走上正轨

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-26
    • 2011-10-01
    • 2010-09-26
    相关资源
    最近更新 更多