数据集中的数据表之间的关系:我们可以检查父级是否是这样吗? [英] The relationship between DataTables in DataSet: can we check if the parent is so and so?

查看:75
本文介绍了数据集中的数据表之间的关系:我们可以检查父级是否是这样吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我加载了一个复杂的XML文件,其中包含大量数据,其中包含复杂级别的嵌套元素. DataSet.ReadXml()很好地加载了所有内容,我可以遍历所有节点.

I loaded a complicated XML file with lots of data where there are complex level of nested elements. The DataSet.ReadXml() load all that nicely and I can loop through all the nodes.

基本上,每个节点都被选择为具有属性作为列名的DataTable.

Essentially each node is picked as DataTable with attributes as column names.

现在,我可以像这样线性地遍历所有这些元素/数据表:

Now I can iterate through ALL these elements/DataTables in a linear fashion like this:

    foreach( DataTable curDT in DS.Tables)
    {
        Console.WriteLine("Table name: {0}",curDT.TableName);
        Console.WriteLine("Columns Count: {0}", curDT.Columns.Count);
        Console.WriteLine("Rows Count: {0}", curDT.Rows.Count);
        Console.WriteLine("Container: {0}", curDT.Container);

        Console.WriteLine("columns =>");

        foreach(DataColumn dataColumn in curDT.Columns)
        {
            Console.WriteLine(dataColumn.ColumnName);
        }

        Console.WriteLine();
    }

但是这些数据表或元素之间具有复杂的嵌套关系(一个在另一个内部).

But these DataTables or elements have complex nested relationships with each other (one is inside another).

当我使用 WriteXml()指定新文件名时,它将正确地重新创建文件.

When I use WriteXml() giving a new file name, it recreates the file correctly.

我的确切问题是 DataSet.Tables 列表之间没有明显的关系,因此如何将所有累积的DataTables放在一个线性列表中却又正确地再现了xml文件.我还想检查某某某某的父母!

My exact question is there is no apparent relationship between the DataSet.Tables list so how is it able to place all cumulative DataTables in one linear list yet they reproduce the xml file correctly. Also I want to check if the parent of so-and-so is this!

更新

我正在解析15000个长的STIG xml文件,该文件可在线访问,但如果有帮助,我将其开头.

I am parsing 15000 long STIG xml file which available online but I will just put the start of it if that helps.

<Benchmark xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cpe="http://cpe.mitre.org/language/2.0" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:dc="http://purl.org/dc/elements/1.1/" id="Windows_10_STIG" xml:lang="en" xsi:schemaLocation="http://checklists.nist.gov/xccdf/1.1 http://nvd.nist.gov/schema/xccdf-1.1.4.xsd http://cpe.mitre.org/dictionary/2.0 http://cpe.mitre.org/files/cpe-dictionary_2.1.xsd" xmlns="http://checklists.nist.gov/xccdf/1.1">
  <status date="2020-06-15">accepted</status>
  <title>Windows 10 Security Technical Implementation Guide</title>
  <description>The Windows 10 Security Technical Implementation Guide (STIG) is published as a tool to improve the security of Department of Defense (DoD) information systems. Comments or proposed revisions to this document should be sent via e-mail to the following address: disa.stig_spt@mail.mil.</description>
  <notice id="terms-of-use" xml:lang="en">Developed_by_DISA_for_the_DoD</notice>
  <reference href="https://cyber.mil/">
    <dc:publisher>DISA</dc:publisher>
    <dc:source>STIG.DOD.MIL</dc:source>
  </reference>
  <plain-text id="release-info">Release: 23 Benchmark Date: 17 Jun 2020</plain-text>
  <version>1</version>
  <Profile id="MAC-1_Classified">
    <title>I - Mission Critical Classified</title>
    <description>&lt;ProfileDescription&gt;&lt;/ProfileDescription&gt;</description>
    <select idref="V-63319" selected="true" />
    <select idref="V-63321" selected="true" />
    <select idref="V-63323" selected="true" />
    <select idref="V-63325" selected="true" />
 </Profile>
  <Group id="V-63319">
    <title>WN10-00-000005</title>
    <description>&lt;GroupDescription&gt;&lt;/GroupDescription&gt;</description>
    <Rule id="SV-77809r3_rule" severity="medium" weight="10.0">
      <version>WN10-00-000005</version>
      <title>Domain-joined systems must use Windows 10 Enterprise Edition 64-bit version.</title>
      <description>&lt;VulnDiscussion&gt;Features such as Credential Guard use virtualization based security to protect information that could be used in credential theft attacks if compromised. There are a number of system requirements that must be met in order for Credential Guard to be configured and enabled properly. Virtualization based security and Credential Guard are only available with Windows 10 Enterprise 64-bit version.&lt;/VulnDiscussion&gt;&lt;FalsePositives&gt;&lt;/FalsePositives&gt;&lt;FalseNegatives&gt;&lt;/FalseNegatives&gt;&lt;Documentable&gt;false&lt;/Documentable&gt;&lt;Mitigations&gt;&lt;/Mitigations&gt;&lt;SeverityOverrideGuidance&gt;&lt;/SeverityOverrideGuidance&gt;&lt;PotentialImpacts&gt;&lt;/PotentialImpacts&gt;&lt;ThirdPartyTools&gt;&lt;/ThirdPartyTools&gt;&lt;MitigationControl&gt;&lt;/MitigationControl&gt;&lt;Responsibility&gt;&lt;/Responsibility&gt;&lt;IAControls&gt;&lt;/IAControls&gt;</description>
      <reference>
        <dc:title>DPMS Target Windows 10</dc:title>
        <dc:publisher>DISA</dc:publisher>
        <dc:type>DPMS Target</dc:type>
        <dc:subject>Windows 10</dc:subject>
        <dc:identifier>2885</dc:identifier>
      </reference>
      <ident system="http://iase.disa.mil/cci">CCI-000366</ident>
      <fixtext fixref="F-69237r2_fix">Use Windows 10 Enterprise 64-bit version for domain-joined systems.</fixtext>
      <fix id="F-69237r2_fix" />
      <check system="C-64053r3_chk">
        <check-content-ref name="M" href="DPMS_XCCDF_Benchmark_Windows_10_STIG.xml" />
        <check-content>
          Verify domain-joined systems are using Windows 10 Enterprise Edition 64-bit version.

          For standalone systems, this is NA.

          Open "Settings".

          Select "System", then "About".

          If "Edition" is not "Windows 10 Enterprise", this is a finding.

          If "System type" is not "64-bit operating system…", this is a finding.
        </check-content>
      </check>
    </Rule>
  </Group>

文件继续打开.我以表示复杂性的方式提供了文件的开头.

The file goes on and on. I have provided the start of the file in way it represents the complexity.

推荐答案

我使用Xml Linq解析xml以使级别数保持较小.使用xml linq,您可以将后代组合到一个对象中.我还使用了类而不是表.参见下面的代码:

I used Xml Linq to parse xml to keep the number of levels small. Using xml linq you can combine descendant into one object. I also used classes instead of table. See code below :

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

namespace ConsoleApplication1
{
    class Program
    {
        const string URL = "https://www.stigviewer.com/stig/windows_10/2020-06-15/MAC-3_Sensitive/xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(URL);
            XElement benchmark = doc.Root;
            XNamespace ns = benchmark.GetDefaultNamespace();
            XNamespace nsDc = benchmark.GetNamespaceOfPrefix("dc");

            Header header = new Header(ns, nsDc, benchmark);
            Profiles profiles = new Profiles(ns, benchmark);
            Group groups = new Group(ns, nsDc, benchmark);

            
        }
    }
    public class Header
    {
        public string status { get; set; }
        public DateTime date { get; set; }
        public string title { get; set; }
        public string description { get; set; }
        public string notice { get; set; }
        public string publisher { get; set; }
        public string source { get; set; }
        public string releaseinfo { get; set; }
        public string version { get; set; }

        public Header(XNamespace ns, XNamespace nsDc, XElement benchmark)
        {
            status = (string)benchmark.Element(ns + "status");
            date = (DateTime)benchmark.Element(ns + "status").Attribute("date");
            title = (string)benchmark.Element(ns + "title");
            description = (string)benchmark.Element(ns + "description");
            notice = (string)benchmark.Element(ns + "notice");
            publisher = (string)benchmark.Descendants(nsDc + "publisher").FirstOrDefault();
            source = (string)benchmark.Descendants(nsDc + "source").FirstOrDefault();
            releaseinfo = (string)benchmark.Element(ns + "plain-text");
            version = (string)benchmark.Element(ns + "version");
        }
    }
    public class Profiles
    {
        public static List<Profiles> ProfileList { get; set; }
        public string id { get; set; }
        public string title { get; set; }
        public string description { get; set; }
        public List<Profile> profiles { get; set; }

        public Profiles() { }
        public Profiles(XNamespace ns, XElement benchmark)
        {
            List<XElement> xProfiles = benchmark.Elements(ns + "Profile").ToList();
            ProfileList = new List<Profiles>();
            foreach (XElement xProfile in xProfiles)
            {
                Profiles Profile = new Profiles();
                ProfileList.Add(Profile);
                Profile.id = (string)xProfile.Attribute("id");
                Profile.title = (string)xProfile.Element(ns + "title");
                Profile.description = (string)xProfile.Element(ns + "description");
                Profile.profiles = xProfile.Elements(ns + "select").Select(x => new Profile()
                {
                    idref = (string)x.Attribute("idref"),
                    selected = (Boolean)x.Attribute("selected")
                }).ToList();
            }
        }

    }
    public class Profile
    {
        public string idref { get; set; }
        public Boolean selected { get; set; }
    }
    public class Group
    {
        public static List<Group> GroupList { get; set; }
        public string id { get; set; }
        public string title { get; set; }
        public string description { get; set; }
        public Rule rule { get; set; }

        public Group() { }
        public Group(XNamespace ns, XNamespace nsDc, XElement benchmark)
        {
            List<XElement> xGroups = benchmark.Elements(ns + "Group").ToList();
            GroupList = new List<Group>();
            foreach (XElement xGroup in xGroups)
            {
                Group group = new Group();
                GroupList.Add(group);
                group.id = (string)xGroup.Attribute("id");
                group.title = (string)xGroup.Element(ns + "title");
                group.description = (string)xGroup.Element(ns + "description");
                XElement xRule = xGroup.Element(ns + "Rule");
                group.rule = new Rule();
                group.rule.id = (string)xRule.Attribute("id");
                group.rule.severity = (string)xRule.Attribute("severity");
                group.rule.weight = (decimal)xRule.Attribute("weight");
                group.rule.version = (string)xRule.Element(ns + "version");
                group.rule.title = (string)xRule.Element(ns + "title");
                group.rule.description = (string)xRule.Element(ns + "description");


                group.rule.idents = xRule.Elements(ns + "idents").Select(x => (string)x).ToList();
                group.rule.fixtext = (string)xRule.Element(ns + "fixtext");
                group.rule.fixref = (string)xRule.Element(ns + "fixtext").Attribute("fixref");
                group.rule.fix = (string)xRule.Element(ns + "fix").Attribute("id");

                XElement xReference = xRule.Element(ns + "reference");
                group.rule.reference = new Reference();
                group.rule.reference.title = (string)xReference.Element(nsDc + "title");
                group.rule.reference.publisher = (string)xReference.Element(nsDc + "publisher");
                group.rule.reference.type = (string)xReference.Element(nsDc + "type");
                group.rule.reference.subject = (string)xReference.Element(nsDc + "subject");
                group.rule.reference.identifier = (string)xReference.Element(nsDc + "identifier"); 


            }
        }
    }
    public class Rule
    {
        public string id { get; set; }
        public string severity { get; set; }
        public decimal weight { get; set; }
        public string version { get; set; }
        public string title { get; set; }
        public string description { get; set; }
        public List<string> idents { get; set; }
        public string fixref { get; set; }
        public string fixtext { get; set; }
        public string fix { get; set; }
        public Reference reference { get; set; }

    }
    public class Reference
    {
        public string title { get; set; }
        public string publisher { get; set; }
        public string type { get; set; }
        public string subject { get; set; }
        public string identifier { get; set; }
    }
}

这篇关于数据集中的数据表之间的关系:我们可以检查父级是否是这样吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆