C# DataSet中数据表之间的关系:我们可以检查父级是否为某某吗?

C# DataSet中数据表之间的关系:我们可以检查父级是否为某某吗?,c#,xml,ado.net,C#,Xml,Ado.net,我加载了一个包含大量数据的复杂XML文件,其中包含复杂级别的嵌套元素。DataSet.ReadXml()很好地加载了所有这些,我就可以遍历所有节点 本质上,每个节点都被选为DataTable,属性作为列名 现在,我可以像这样以线性方式迭代所有这些元素/数据表: foreach( DataTable curDT in DS.Tables) { Console.WriteLine("Table name: {0}",curDT.TableName)

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

本质上,每个节点都被选为DataTable,属性作为列名

现在,我可以像这样以线性方式迭代所有这些元素/数据表:

    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();
    }
但是这些数据表或元素彼此之间有复杂的嵌套关系(一个在另一个内部)

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

我的确切问题是,
DataSet.Tables
列表之间没有明显的关系,因此如何才能将所有累积数据表放在一个线性列表中,而它们又能正确地复制xml文件。我还想检查某某的父母是否是这个

更新

我正在解析15000长的STIG xml文件,该文件可以在线获得,但如果有帮助的话,我会把它放在开头

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

认可的
Windows 10安全技术实施指南
《Windows 10安全技术实施指南》(STIG)是作为提高国防部(DoD)信息系统安全性的工具发布的。对本文件的意见或建议修订应通过电子邮件发送至以下地址:disa.stig_spt@mail.mil.
由国防部开发
迪萨
STIG.DOD.MIL
发布日期:23基准日期:2020年6月17日
1.
I-关键任务分类
ProfileDescription/ProfileDescription
WN10-00-000005
GroupDescription/GroupDescription
WN10-00-000005
加入域的系统必须使用Windows 10 Enterprise Edition 64位版本。
VulnDiscussionFeatures(如Credential Guard)使用基于虚拟化的安全性来保护信息,这些信息在受到攻击时可能会被用于凭证盗窃攻击。要正确配置和启用凭证保护,必须满足许多系统要求。基于虚拟化的安全和凭据保护仅适用于Windows 10 Enterprise 64位版本./Vulndiscussion误报/误报误报/误报文档False/DocumentableFalse/DocumentableMablemitizations/Reductions验证过度指导/严重过度指导潜在影响/潜在影响第三方工具/第三方工具污染控制/缓解控制责任/责任控制/IAControl
DPMS目标Windows 10
迪萨
DPMS目标
视窗10
2885
CCI-000366
对于加入域的系统,请使用Windows 10 Enterprise 64位版本。
验证加入域的系统是否使用Windows 10 Enterprise Edition 64位版本。
对于独立系统,这是NA。
打开“设置”。
选择“系统”,然后选择“关于”。
如果“Edition”不是“Windows 10 Enterprise”,则这是一个查找。
如果“系统类型”不是“64位操作系统…”,则这是一个查找。

这个文件没完没了。我以表示复杂性的方式提供了文件的开头。

啊。但你的问题是它是如何做到的?格式良好的XML是可解析的,为什么他们可以在XML中释放15000多行代码来解析和利用

至于它是线性的还是锯齿状的,它是线性的,这意味着它将按照你所拥有的从开始到结束进行处理。如果该XML文件有/有1个无效部分,它将无法读取。您可以通过移除某个标记来测试这一点。它应该抛出一个异常


如果您正在寻找一种不将其全部加载到内存中的方法,给定大小,请查看此答案:。如果这不是你问题背后的原因,那么请告诉我们,我们可以看看我们是否能够弄清楚你在追求什么。

我使用Xml Linq解析Xml,以保持级别数很小。使用XMLLINQ,您可以将子体组合到一个对象中。我还使用了类而不是表。见下面的代码:

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; }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序1
{
班级计划
{
常量字符串URL=”https://www.stigviewer.com/stig/windows_10/2020-06-15/MAC-3_Sensitive/xml";
静态void Main(字符串[]参数)
{
XDocument doc=XDocument.Load(URL);
XElement benchmark=doc.Root;
XNamespace ns=benchmark.GetDefaultNamespace();
XNamespace nsDc=benchmark.GetNamespaceOfPrefix(“dc”);
Header Header=新的Header(ns、nsDc、基准);
配置文件=新配置文件(ns,基准);
组组=新组(ns、nsDc、基准);
}
}
公共类标题
{
公共字符串状态{get;set;}
公共日期时间日期{get;set;}
公共字符串标题{get;set;}
公共字符串说明{get;set;}
公共字符串通知{get;set;}
公共字符串发布程序{get;set;}
公共字符串源{get;set;}
公共字符串releaseinfo{get;set;}
公共字符串版本{get;set;}
公共头(XNamespace ns、XNamespace nsDc、XElement基准)
{
status=(字符串)benchmark.Element(ns+“status”);
date=(DateTime)benchmark.Element(ns+“status”).Attribute(“日期”);
title=(字符串)benchmark.Element(ns+“title”);
description=(字符串)benchmark.Element(ns+“description”);
notice=(字符串)benchmark.Element(ns+“notice”);
publisher=(字符串)benchmark.substands(nsDc+“publisher”).FirstOrDefault();
source=(字符串)benchmark.substands(nsDc+“source”).FirstOrD