C#LINQ和XML解析具有单独的部分

时间:2016-10-28 01:23:12

标签: c# xml linq

我正在尝试编写的程序遇到一些麻烦。它将使用由另一个程序生成的XML文件,因此格式将始终相同,但部分中的部分和数据的数量将不同,我试图使其成为通用。

以下是XML示例:

<?xml version="1.0" encoding="utf-8" ?>
<hcdata>
  <docTitle>Test Health check</docTitle>
  <sections>
    <section id="1" name="server-overview">
      <h1>Server Overview</h1>
      <table name="server1">
        <th>Field</th>
        <th>Value</th>
        <tr>
          <td>Name</td>
          <td>TestESXI1</td>
        </tr>
        <tr>
          <td>RAM</td>
          <td>24GB</td>
        </tr>
      </table>
      <table name="server2">
        <th>Field</th>
        <th>Value</th>
        <tr>
          <td>Name</td>
          <td>TestESXI2</td>
        </tr>
        <tr>
          <td>RAM</td>
          <td>16GB</td>
        </tr>
      </table>
    </section>
    <section id="2" name="vms">
      <h1>Virtual Machine Information</h1>
      <table name="vminfo">
        <th>VM Name</th>
        <th>RAM Usage</th>
        <tr>
          <td>2K8R2</td>
          <td>2048MB</td>
        </tr>
        <tr>
          <td>2K12R2</td>
          <td>4096Mb</td>
        </tr>
      </table>
    </section>
  </sections>
</hcdata>

这里有一些C#代码,我一直在努力尝试拉取值:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace XMLParseDev
{
    class XMLParseDev
    {
        static void Main(string[] args)
        {
            int sectionCount = 0;
            Console.WriteLine(sectionCount);

            XDocument xDoc = XDocument.Load(@"C:\Users\test.xml");
            //XElement xEle = XElement.Load(@"C:\users\test.xml");
            //Application winWord = new Application();

            IEnumerable<XElement> xElements = xDoc.Elements();
            IEnumerable<XElement> xSectionCount = from xSections in xDoc.Descendants("section") select xSections;
            IEnumerable<XElement> xthCount = from xth in xDoc.Descendants("th") select xth;

            foreach (XElement s in xSectionCount)
            {
                //This is to count the number of <section> tags, this part works
                sectionCount = sectionCount + 1;

                //This was trying to write the value of the <h1> tag but does not
                IEnumerable<XElement> xH1 = from xH1Field in xDoc.Descendants("h1") select xH1Field;
                Console.WriteLine(xH1.Attributes("h1"));

                foreach (XElement th in xthCount)
                {
                    //This was supposed to write the <th> value only for <th> within the <section> but writes them all
                    Console.WriteLine(th.Value);
                }
            }
            Console.WriteLine(sectionCount);
        }
    }
}

输出:

0
System.Xml.Linq.Extensions+<GetAttributes>d__1
Field
Value
Field
Value
VM Name
RAM Usage
System.Xml.Linq.Extensions+<GetAttributes>d__1
Field
Value
Field
Value
VM Name
RAM Usage
2

基本上我想要做的是将XML转换为Word文档(这个问题不是关于Word部分,只是数据获取)。我使用类似于HTML的标签来帮助简化设计 我需要将每个<section>标记作为单个部分进行处理。 我计划通过运行,因此我可以获得表行和列的计数,因此可以创建表然后填充表(因为表需要首先使用正确的尺寸)。 该部分还将有一个标题(<h1>)。

我计划将此运行作为一个循环,它将循环部分并在迭代中执行此部分中的所有其他操作,但我无法弄清楚如何将数据选择锁定到特定部分。

希望这是有意义的,并提前感谢。

1 个答案:

答案 0 :(得分:1)

我想知道您是否可能更容易让DataSet将数据解析为DataTables然后选择您想要数据的表。这是一个小片段,它将读取xml文件并将所有数据显示为表格:

DataSet ds = new DataSet();
ds.ReadXml("xmlfile2.xml");
foreach(DataTable dt in ds.Tables)
{
    Console.WriteLine($"Table Name - {dt.TableName}\n");
    foreach(DataColumn dc in dt.Columns)
    {
        Console.Write($"{dc.ColumnName.PadRight(16)}");
    }
    Console.WriteLine();
    foreach(DataRow dr in dt.Rows)
    {

        foreach(object obj in dr.ItemArray)
        {
            Console.Write($"{obj.ToString().PadRight(16)}");
        }
        Console.WriteLine();
    }
    Console.WriteLine(new string('_', 75));
}
相关问题