简体   繁体   English

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

[英]C# LINQ and XML Parsing with Separate Sections

I am having a bit of trouble with a program I am trying to write. 我尝试编写的程序遇到麻烦。 It is going to be using XML files that are generated by another program, so the formatting will always be the same, but number of sections and data within a section will be different, and I am trying to make it universal. 它将使用由另一个程序生成的XML文件,因此格式将始终相同,但是节的数量和节中的数据将有所不同,我正在尝试使其具有通用性。

Here is a sample 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>

And here is some C# code I have been messing around with to try and pull values: 这是我一直在弄乱的一些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);
        }
    }
}

And the output: 并输出:

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

Basically what I want to do, is convert the XML to a Word document (this question isn't about the Word part, just the data getting). 基本上,我想做的是将XML转换为Word文档(此问题与Word部分无关,而与数据获取有关)。 I've used tags similar to HTML to assist with ease of design. 我使用了类似于HTML的标签来简化设计。
I need each <section> tag to be processed as an individual part. 我需要将每个<section>标记作为一个单独的部分进行处理。 I planned on running through so I can get counts of table rows and columns, so the table can be created and then populated (as the table needs to be made with the right dimensions first). 我计划进行遍历,以便获得表行和列的计数,以便可以创建然后填充表(因为首先需要使用正确的尺寸来制作表)。
The section will also have a heading ( <h1> ). 该部分还将具有标题( <h1> )。

I planned on this running as a loop that would be a foreach that loops sections and does everything else within this section in the iteration, but I can't figure out how to lock the data selection down to just a specific section. 我计划将其作为循环运行,这将是一个foreach,用于循环部分并在迭代中执行此部分中的所有其他操作,但是我无法弄清楚如何将数据选择锁定到特定的部分。

Hope this makes sense and thanks in advance. 希望这是有道理的,并预先感谢。

I'm wondering if you might find it easier to let a DataSet parse the data into DataTables then pick which tables you want the data from. 我想知道您是否发现让DataSet将数据解析为DataTables,然后选择要从中获取数据的表更容易。 Here's a little snippet that will read the xml file and display all the data as tables: 这是一个小片段,它将读取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));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM