将特定的xml元素转换为datatable C#

时间:2015-06-17 13:21:33

标签: c# xml powershell anonymous-objects

我有一个powershell脚本,可将特定的xml元素转换为CSV。我想利用C#扩展功能。 powershell脚本如下:

{
    [xml]$inp = get-content $infile
        $inp.config.devices.entry."device-group".entry |
        foreach {
            $vsys = $_.name
            $seq = 0   
            $_.$rulebase.security.rules.entry | 
                foreach { 
                    New-Object psobject -property @{
                    Vsys = $vsys ;
                    Seq = ++$seq;
                    Name = $_.name; 
                    From = fmat($_.from.member);
                    To = fmat($_.to.member);
                    Source = fmat($_.source.member);
                    Destination = fmat($_.destination.member);
                    SourceUser = fmat($_."source-user".member);
                    Category = fmat($_.category.member);
                    Application = fmat($_.application.member);
                    Service = fmat($_.service.member);
                    Action = $_.action;
                    NegateSource = $_."negate-source";
                    NegateDestination = $_."negate-destination"
                    Tag = fmat($_.tag.member); 
                    Target = fmat_vsys($_.target.devices) }  
                } 
        } | select Vsys, Seq, Name, From, Source, NegateSource, SourceUser, To, Destination, NegateDestination, Application, Service, Category, Action, Tag, Target | 
            export-csv $outfile
        Start-Process $outfile
    }

到目前为止,我的C#看起来像这样:

try {
    var xdoc = XDocument.Load(filename);

    var results = xdoc.Elements("config")
        .Elements("devices")
        .Elements("entry")
        .Elements("vsys")
        .Elements("entry")
        .Elements("rulebase")
        .Elements("security")
        .Elements("rules")
        .Elements("entry")
        .Descendants()
        .Select(e => new {ElementName = e.Name, ElementValue = e.Value});

我的问题是,如何将数据从匿名对象中获取到数据表中,以便我的结果基于行?

这是XML的一个示例

<rulebase>
            <security>
              <rules>
                <entry name="">
                  <from>
                    <member></member>
                  </from>
                  <to>
                    <member></member>
                  </to>
                  <source>
                    <member></member>
                  </source>
                  <destination>
                    <member></member>
                  </destination>
                  <source-user>
                    <member></member>
                  </source-user>
                  <category>
                    <member></member>
                  </category>
                  <application>
                    <member></member>
                    <member></member>
                    <member></member>
                    <member></member>
                  </application>
                  <service>
                    <member></member>
                  </service>
                  <hip-profiles>
                    <member></member>
                  </hip-profiles>
                  <action>deny</action>
                  <description></description>
                  <log-setting></log-setting>
                </entry>
                </rules>
                </security>
                </rulebase>

0 个答案:

没有答案