反序列化 XML / GPO?
Deserialize XML / GPO?
我尝试用 C# 读取 GPO。到目前为止,我收到以下 XML-String:
"<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n<GPO xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"http://www.microsoft.com/GroupPolicy/Settings\">\r\n <Identifier>\r\n <Identifier xmlns=\"http://www.microsoft.com/GroupPolicy/Types\">{31b2f340-016d-11d2-945f-00c04fb984f9}</Identifier>\r\n <Domain xmlns=\"http://www.microsoft.com/GroupPolicy/Types\">test.local</Domain>\r\n </Identifier>\r\n <Name>Default Domain Policy</Name>\r\n <IncludeComments>true</IncludeComments>\r\n <CreatedTime>2020-09-30T08:29:25</CreatedTime>\r\n <ModifiedTime>2020-09-30T08:45:08</ModifiedTime>\r\n <ReadTime>2020-10-25T14:05:29.4338122Z</ReadTime>\r\n <SecurityDescriptor>\r\n <SDDL xmlns=\"http://www.microsoft.com/GroupPolicy/Types/Security\">O:DAG:DAD:PAI(A;;CCLCSWRPWPLORCWDWO;;;DA)(A;CIIO;CCDCLCSWRPWPDTLOSDRCWDWO;;;DA)(A;;CCLCSWRPWPLORCWDWO;;;S-1-5-21-3293055155-3023367737-2546885349-519)(A;CIIO;CCDCLCSWRPWPDTLOSDRCWDWO;;;S-1-5-21-3293055155-3023367737-2546885349-519)(A;;CCLCSWRPWPLORCWDWO;;;DA)(A;CIIO;CCDCLCSWRPWPDTLOSDRCWDWO;;;CO)(A;CI;CCDCLCSWRPWPDTLOSDRCWDWO;;;SY)(A;CI;LCRPLORC;;;AU)(OA;CI;CR;edacfd8f-ffb3-11d1-b41d-00a0c968f939;;AU)(A;CI;LCRPLORC;;;ED)S:AI(OU;CIIDSA;WPWD;;f30e3bc2-9ff0-11d1-b603-0000f80367c1;WD)(OU;CIIOIDSA;WP;f30e3bbe-9ff0-11d1-b603-0000f80367c1;bf967aa5-0de6-11d0-a285-00aa003049e2;WD)(OU;CIIOIDSA;WP;f30e3bbf-9ff0-11d1-b603-0000f80367c1;bf967aa5-0de6-11d0-a285-00aa003049e2;WD)</SDDL>\r\n <Owner xmlns=\"http://www.microsoft.com/GroupPolicy/Types/Security\">\r\n <SID xmlns=\"http://www.microsoft.com/GroupPolicy/Types\">S-1-5-21-3293055155-3023367737-2546885349-512</SID>\r\n <Name xmlns=\"http://www.microsoft.com/GroupPolicy/Types\">TEST\Domänen-Admins</Name>\r\n </Owner>\r\n <Group xmlns=\"http://www.microsoft.com/GroupPolicy/Types/Security\">\r\n <SID xmlns=\"http://www.microsoft.com/GroupPolicy/Types\">S-1-5-21-3293055155-3023367737-2546885349-512</SID>\r\n <Name xmlns=\"http://www.microsoft.com/GroupPolicy/Types\">TEST\Domänen-Admins</Name>\r\n </Group>\r\n <PermissionsPresent xmlns=\"http://www.microsoft.com/GroupPolicy/Types/Security\">true</PermissionsPresent>\r\n <Permissions xmlns=\"http://www.microsoft.com/GroupPolicy/Types/Security\">\r\n <InheritsFromParent>false</InheritsFromParent>\r\n <TrusteePermissions>\r\n <Trustee>\r\n <SID xmlns=\"http://www.microsoft.com/GroupPolicy/Types\">S-1-5-9</SID>\r\n <Name xmlns=\"http://www.microsoft.com/GroupPolicy/Types\">NT AUTHORITY\ENTERPRISE DOMAIN CONTROLLERS</Name>\r\n </Trustee>\r\n <Type xsi:type=\"PermissionType\">\r\n <PermissionType>Allow</PermissionType>\r\n </Type>\r\n <Inherited>false</Inherited>\r\n <Applicability>\r\n <ToSelf>true</ToSelf>\r\n <ToDescendantObjects>false</ToDescendantObjects>\r\n <ToDescendantContainers>true</ToDescendantContainers>\r\n <ToDirectDescendantsOnly>false</ToDirectDescendantsOnly>\r\n </Applicability>\r\n <Standard>\r\n <GPOGroupedAccessEnum>Read</GPOGroupedAccessEnum>\r\n </Standard>\r\n <AccessMask>0</AccessMask>\r\n </TrusteePermissions>\r\n <TrusteePermissions>\r\n <Trustee>\r\n <SID xmlns=\"http://www.microsoft.com/GroupPolicy/Types\">S-1-5-11</SID>\r\n <Name xmlns=\"http://www.microsoft.com/GroupPolicy/Types\">NT AUTHORITY\Authenticated Users</Name>\r\n </Trustee>\r\n <Type xsi:type=\"PermissionType\">\r\n <PermissionType>Allow</PermissionType>\r\n </Type>\r\n <Inherited>false</Inherited>\r\n <Applicability>\r\n <ToSelf>true</ToSelf>\r\n <ToDescendantObjects>false</ToDescendantObjects>\r\n <ToDescendantContainers>true</ToDescendantContainers>\r\n <ToDirectDescendantsOnly>false</ToDirectDescendantsOnly>\r\n </Applicability>\r\n <Standard>\r\n <GPOGroupedAccessEnum>Apply Group Policy</GPOGroupedAccessEnum>\r\n </Standard>\r\n <AccessMask>0</AccessMask>\r\n </TrusteePermissions>\r\n <TrusteePermissions>\r\n <Trustee>\r\n <SID xmlns=\"http://www.microsoft.com/GroupPolicy/Types\">S-1-5-18</SID>\r\n <Name xmlns=\"http://www.microsoft.com/GroupPolicy/Types\">NT AUTHORITY\SYSTEM</Name>\r\n </Trustee>\r\n <Type xsi:type=\"PermissionType\">\r\n <PermissionType>Allow</PermissionType>\r\n </Type>\r\n <Inherited>false</Inherited>\r\n <Applicability>\r\n <ToSelf>true</ToSelf>\r\n <ToDescendantObjects>false</ToDescendantObjects>\r\n <ToDescendantContainers>true</ToDescendantContainers>\r\n <ToDirectDescendantsOnly>false</ToDirectDescendantsOnly>\r\n </Applicability>\r\n <Standard>\r\n <GPOGroupedAccessEnum>Edit, delete, modify security</GPOGroupedAccessEnum>\r\n </Standard>\r\n <AccessMask>0</AccessMask>\r\n </TrusteePermissions>\r\n </Permissions>\r\n <AuditingPresent xmlns=\"http://www.microsoft.com/GroupPolicy/Types/Security\">false</AuditingPresent>\r\n </SecurityDescriptor>\r\n <FilterDataAvailable>true</FilterDataAvailable>\r\n <Computer>\r\n <VersionDirectory>3</VersionDirectory>\r\n <VersionSysvol>3</VersionSysvol>\r\n <Enabled>true</Enabled>\r\n <ExtensionData>\r\n <Extension xmlns:q1=\"http://www.microsoft.com/GroupPolicy/Settings/Security\" xsi:type=\"q1:SecuritySettings\">\r\n <q1:Account>\r\n <q1:Name>ClearTextPassword</q1:Name>\r\n <q1:SettingBoolean>false</q1:SettingBoolean>\r\n <q1:Type>Password</q1:Type>\r\n </q1:Account>\r\n <q1:Account>\r\n <q1:Name>LockoutBadCount</q1:Name>\r\n <q1:SettingNumber>0</q1:SettingNumber>\r\n <q1:Type>Account Lockout</q1:Type>\r\n </q1:Account>\r\n <q1:Account>\r\n <q1:Name>MaximumPasswordAge</q1:Name>\r\n <q1:SettingNumber>42</q1:SettingNumber>\r\n <q1:Type>Password</q1:Type>\r\n </q1:Account>\r\n <q1:Account>\r\n <q1:Name>MinimumPasswordAge</q1:Name>\r\n <q1:SettingNumber>1</q1:SettingNumber>\r\n <q1:Type>Password</q1:Type>\r\n </q1:Account>\r\n <q1:Account>\r\n <q1:Name>MinimumPasswordLength</q1:Name>\r\n <q1:SettingNumber>7</q1:SettingNumber>\r\n <q1:Type>Password</q1:Type>\r\n </q1:Account>\r\n <q1:Account>\r\n <q1:Name>PasswordComplexity</q1:Name>\r\n <q1:SettingBoolean>true</q1:SettingBoolean>\r\n <q1:Type>Password</q1:Type>\r\n </q1:Account>\r\n <q1:Account>\r\n <q1:Name>PasswordHistorySize</q1:Name>\r\n <q1:SettingNumber>24</q1:SettingNumber>\r\n <q1:Type>Password</q1:Type>\r\n </q1:Account>\r\n <q1:Account>\r\n <q1:Name>MaxClockSkew</q1:Name>\r\n <q1:SettingNumber>5</q1:SettingNumber>\r\n <q1:Type>Kerberos</q1:Type>\r\n </q1:Account>\r\n <q1:Account>\r\n <q1:Name>MaxRenewAge</q1:Name>\r\n <q1:SettingNumber>7</q1:SettingNumber>\r\n <q1:Type>Kerberos</q1:Type>\r\n </q1:Account>\r\n <q1:Account>\r\n <q1:Name>MaxServiceAge</q1:Name>\r\n <q1:SettingNumber>600</q1:SettingNumber>\r\n <q1:Type>Kerberos</q1:Type>\r\n </q1:Account>\r\n <q1:Account>\r\n <q1:Name>MaxTicketAge</q1:Name>\r\n <q1:SettingNumber>10</q1:SettingNumber>\r\n <q1:Type>Kerberos</q1:Type>\r\n </q1:Account>\r\n <q1:Account>\r\n <q1:Name>TicketValidateClient</q1:Name>\r\n <q1:SettingBoolean>true</q1:SettingBoolean>\r\n <q1:Type>Kerberos</q1:Type>\r\n </q1:Account>\r\n <q1:SecurityOptions>\r\n <q1:KeyName>MACHINE\System\CurrentControlSet\Control\Lsa\NoLMHash</q1:KeyName>\r\n <q1:SettingNumber>1</q1:SettingNumber>\r\n <q1:Display>\r\n <q1:Name>Network security: Do not store LAN Manager hash value on next password change</q1:Name>\r\n <q1:Units />\r\n <q1:DisplayBoolean>true</q1:DisplayBoolean>\r\n </q1:Display>\r\n </q1:SecurityOptions>\r\n <q1:SecurityOptions>\r\n <q1:SystemAccessPolicyName>ForceLogoffWhenHourExpire</q1:SystemAccessPolicyName>\r\n <q1:SettingNumber>0</q1:SettingNumber>\r\n </q1:SecurityOptions>\r\n <q1:SecurityOptions>\r\n <q1:SystemAccessPolicyName>LSAAnonymousNameLookup</q1:SystemAccessPolicyName>\r\n <q1:SettingNumber>0</q1:SettingNumber>\r\n </q1:SecurityOptions>\r\n <q1:Blocked>false</q1:Blocked>\r\n </Extension>\r\n <Name>Security</Name>\r\n </ExtensionData>\r\n <ExtensionData>\r\n <Extension xmlns:q2=\"http://www.microsoft.com/GroupPolicy/Settings/PublicKey\" xsi:type=\"q2:PublicKeySettings\">\r\n <q2:EFSSettings>\r\n <q2:AllowEFS>2</q2:AllowEFS>\r\n <q2:Options>0</q2:Options>\r\n <q2:CacheTimeout>0</q2:CacheTimeout>\r\n <q2:KeyLen>0</q2:KeyLen>\r\n </q2:EFSSettings>\r\n <q2:EFSRecoveryAgent>\r\n <q2:IssuedTo>Administrator</q2:IssuedTo>\r\n <q2:IssuedBy>Administrator</q2:IssuedBy>\r\n <q2:ExpirationDate>2120-09-06T08:45:08Z</q2:ExpirationDate>\r\n <q2:CertificatePurpose>\r\n <q2:Purpose>1.3.6.1.4.1.311.10.3.4.1</q2:Purpose>\r\n </q2:CertificatePurpose>\r\n <q2:Data>0200000001000000CC0000001C0000006C0000000100000000000000000000000000000001000000610031003900650039003000350033002D0036003200650037002D0034003000610061002D0038006300620035002D0065003200300032006500300031003300620033006100310000000000000000004D006900630072006F0073006F0066007400200045006E00680061006E006300650064002000430072007900700074006F0067007200610070006800690063002000500072006F00760069006400650072002000760031002E00300000000000030000000100000014000000B664183C38ED8EBA8CBAD8438B28D73FCC9A86BC2000000001000000850300003082038130820269A00302010202103F608D5FC797B2BF480FC0B8C36D13A2300D06092A864886F70D01010505003050311630140603550403130D41646D696E6973747261746F72310C300A0603550407130345465331283026060355040B131F4546532046696C6520456E6372797074696F6E2043657274696669636174653020170D3230303933303038343530385A180F32313230303930363038343530385A3050311630140603550403130D41646D696E6973747261746F72310C300A0603550407130345465331283026060355040B131F4546532046696C6520456E6372797074696F6E20436572746966696361746530820122300D06092A864886F70D01010105000382010F003082010A0282010100CE0D82D3E5411B73D06675FF6222B598598D70154CEF9D3421378E3C250047342F9CA97FA2BC4B32D5294496910454A1DF19E3E2932BC03E691D0797D5F59184A895A0AF73B49F0DD0417D008AB2F766332D0C31FA004F365E395B7CBC02C41E8B3783709F53BDDE8826047227F6AB54C2EF138D4F85270D74D6CC8B3BFB1ED836161A5A297860BD3EDF50031634985E12D38D4425384F6B6F855C4D64C7CE4E5F7BF6077AB6A9242204CAA6E060DD381EC77DD6FACF7533F1ED49860F2BB6000C9F20F53FEB7D28A6FA4CCCBCC8ECD851BC12EA820B2AE28F2F0A0AA8A5853417A1A31DBBC976B2955874D6E1C61702A97453CAAD283AA1A914D9BE11B2193D0203010001A355305330160603551D25040F300D060B2B0601040182370A030401302E0603551D1104273025A023060A2B060104018237140203A0150C1341646D696E6973747261746F7240544553540030090603551D1304023000300D06092A864886F70D0101050500038201010045ADC5612E87B49FEFAD5A3A84768A495344D5ACC9D34CAA5C635CB42B1FFB76EAA123712BD3FD113F4AC63B01062A22BAE6D6DCF208035DEF8BCE561B035B1C9087614EEF8A193A216C766CD20DC695870FBDA67F6142B7682E962025436F15D75ED4B081B587AFEE93683800BB1DC2533F1D31141D764999300F1F42B1A778A9226D5500746D782B591153F172F1FC21D175154C31BD9F41B97428870E3A3CEC1EBA05624335C43C24D00BBCFD3ECD939D120C02E5C19D0BC04F81922CC8B92E37F907E2984F710F25C6FB6D38BEC874239CD4FFBA76B66893F397B5A82B5069E57B9A82A454B7108F60E5DF697C7ABAAEF7B32DB7BEB449A2CD66132609B3</q2:Data>\r\n </q2:EFSRecoveryAgent>\r\n <q2:RootCertificateSettings>\r\n <q2:AllowNewCAs>true</q2:AllowNewCAs>\r\n <q2:TrustThirdPartyCAs>true</q2:TrustThirdPartyCAs>\r\n <q2:RequireUPNNamingConstraints>false</q2:RequireUPNNamingConstraints>\r\n </q2:RootCertificateSettings>\r\n </Extension>\r\n <Name>Public Key</Name>\r\n </ExtensionData>\r\n <ExtensionData>\r\n <Extension xmlns:q3=\"http://www.microsoft.com/GroupPolicy/Settings/Registry\" xsi:type=\"q3:RegistrySettings\">\r\n <q3:Blocked>false</q3:Blocked>\r\n </Extension>\r\n <Name>Registry</Name>\r\n </ExtensionData>\r\n </Computer>\r\n <User>\r\n <VersionDirectory>0</VersionDirectory>\r\n <VersionSysvol>0</VersionSysvol>\r\n <Enabled>true</Enabled>\r\n </User>\r\n <LinksTo>\r\n <SOMName>test</SOMName>\r\n <SOMPath>test.local</SOMPath>\r\n <Enabled>true</Enabled>\r\n <NoOverride>false</NoOverride>\r\n </LinksTo>\r\n</GPO>"
现在我想读它/想读出一些价值观。例如,如果我想从“MaxServiceAge”(值为 600)中读取值,我该怎么做?
此致,
亨德里克
使用 Xml Linq:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
string xml = File.ReadAllText(FILENAME);
XDocument doc = XDocument.Parse(xml);
List<XElement> xAccounts = doc.Descendants().Where(x => x.Name.LocalName == "Account").ToList();
XNamespace q1 = xAccounts.FirstOrDefault().GetNamespaceOfPrefix("q1");
Dictionary<string, Account> accountDict = xAccounts.Select(x => new Account()
{
Name = (string)x.Element(q1 + "Name"),
Setting = x.Elements().Where(y => y.Name.LocalName.StartsWith("Setting")).Select(y => new KeyValuePair<string,string>(y.Name.LocalName, (string)y)).FirstOrDefault(),
Type = (string)x.Element(q1 + "Type")
})
.GroupBy(x => x.Name, y => y)
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
Account MaxServiceAge = accountDict["MaxServiceAge"];
}
}
public class Account
{
public string Name { get; set;}
public KeyValuePair<string,string> Setting { get; set;}
public string Type { get; set;}
}
}
我尝试用 C# 读取 GPO。到目前为止,我收到以下 XML-String:
"<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n<GPO xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"http://www.microsoft.com/GroupPolicy/Settings\">\r\n <Identifier>\r\n <Identifier xmlns=\"http://www.microsoft.com/GroupPolicy/Types\">{31b2f340-016d-11d2-945f-00c04fb984f9}</Identifier>\r\n <Domain xmlns=\"http://www.microsoft.com/GroupPolicy/Types\">test.local</Domain>\r\n </Identifier>\r\n <Name>Default Domain Policy</Name>\r\n <IncludeComments>true</IncludeComments>\r\n <CreatedTime>2020-09-30T08:29:25</CreatedTime>\r\n <ModifiedTime>2020-09-30T08:45:08</ModifiedTime>\r\n <ReadTime>2020-10-25T14:05:29.4338122Z</ReadTime>\r\n <SecurityDescriptor>\r\n <SDDL xmlns=\"http://www.microsoft.com/GroupPolicy/Types/Security\">O:DAG:DAD:PAI(A;;CCLCSWRPWPLORCWDWO;;;DA)(A;CIIO;CCDCLCSWRPWPDTLOSDRCWDWO;;;DA)(A;;CCLCSWRPWPLORCWDWO;;;S-1-5-21-3293055155-3023367737-2546885349-519)(A;CIIO;CCDCLCSWRPWPDTLOSDRCWDWO;;;S-1-5-21-3293055155-3023367737-2546885349-519)(A;;CCLCSWRPWPLORCWDWO;;;DA)(A;CIIO;CCDCLCSWRPWPDTLOSDRCWDWO;;;CO)(A;CI;CCDCLCSWRPWPDTLOSDRCWDWO;;;SY)(A;CI;LCRPLORC;;;AU)(OA;CI;CR;edacfd8f-ffb3-11d1-b41d-00a0c968f939;;AU)(A;CI;LCRPLORC;;;ED)S:AI(OU;CIIDSA;WPWD;;f30e3bc2-9ff0-11d1-b603-0000f80367c1;WD)(OU;CIIOIDSA;WP;f30e3bbe-9ff0-11d1-b603-0000f80367c1;bf967aa5-0de6-11d0-a285-00aa003049e2;WD)(OU;CIIOIDSA;WP;f30e3bbf-9ff0-11d1-b603-0000f80367c1;bf967aa5-0de6-11d0-a285-00aa003049e2;WD)</SDDL>\r\n <Owner xmlns=\"http://www.microsoft.com/GroupPolicy/Types/Security\">\r\n <SID xmlns=\"http://www.microsoft.com/GroupPolicy/Types\">S-1-5-21-3293055155-3023367737-2546885349-512</SID>\r\n <Name xmlns=\"http://www.microsoft.com/GroupPolicy/Types\">TEST\Domänen-Admins</Name>\r\n </Owner>\r\n <Group xmlns=\"http://www.microsoft.com/GroupPolicy/Types/Security\">\r\n <SID xmlns=\"http://www.microsoft.com/GroupPolicy/Types\">S-1-5-21-3293055155-3023367737-2546885349-512</SID>\r\n <Name xmlns=\"http://www.microsoft.com/GroupPolicy/Types\">TEST\Domänen-Admins</Name>\r\n </Group>\r\n <PermissionsPresent xmlns=\"http://www.microsoft.com/GroupPolicy/Types/Security\">true</PermissionsPresent>\r\n <Permissions xmlns=\"http://www.microsoft.com/GroupPolicy/Types/Security\">\r\n <InheritsFromParent>false</InheritsFromParent>\r\n <TrusteePermissions>\r\n <Trustee>\r\n <SID xmlns=\"http://www.microsoft.com/GroupPolicy/Types\">S-1-5-9</SID>\r\n <Name xmlns=\"http://www.microsoft.com/GroupPolicy/Types\">NT AUTHORITY\ENTERPRISE DOMAIN CONTROLLERS</Name>\r\n </Trustee>\r\n <Type xsi:type=\"PermissionType\">\r\n <PermissionType>Allow</PermissionType>\r\n </Type>\r\n <Inherited>false</Inherited>\r\n <Applicability>\r\n <ToSelf>true</ToSelf>\r\n <ToDescendantObjects>false</ToDescendantObjects>\r\n <ToDescendantContainers>true</ToDescendantContainers>\r\n <ToDirectDescendantsOnly>false</ToDirectDescendantsOnly>\r\n </Applicability>\r\n <Standard>\r\n <GPOGroupedAccessEnum>Read</GPOGroupedAccessEnum>\r\n </Standard>\r\n <AccessMask>0</AccessMask>\r\n </TrusteePermissions>\r\n <TrusteePermissions>\r\n <Trustee>\r\n <SID xmlns=\"http://www.microsoft.com/GroupPolicy/Types\">S-1-5-11</SID>\r\n <Name xmlns=\"http://www.microsoft.com/GroupPolicy/Types\">NT AUTHORITY\Authenticated Users</Name>\r\n </Trustee>\r\n <Type xsi:type=\"PermissionType\">\r\n <PermissionType>Allow</PermissionType>\r\n </Type>\r\n <Inherited>false</Inherited>\r\n <Applicability>\r\n <ToSelf>true</ToSelf>\r\n <ToDescendantObjects>false</ToDescendantObjects>\r\n <ToDescendantContainers>true</ToDescendantContainers>\r\n <ToDirectDescendantsOnly>false</ToDirectDescendantsOnly>\r\n </Applicability>\r\n <Standard>\r\n <GPOGroupedAccessEnum>Apply Group Policy</GPOGroupedAccessEnum>\r\n </Standard>\r\n <AccessMask>0</AccessMask>\r\n </TrusteePermissions>\r\n <TrusteePermissions>\r\n <Trustee>\r\n <SID xmlns=\"http://www.microsoft.com/GroupPolicy/Types\">S-1-5-18</SID>\r\n <Name xmlns=\"http://www.microsoft.com/GroupPolicy/Types\">NT AUTHORITY\SYSTEM</Name>\r\n </Trustee>\r\n <Type xsi:type=\"PermissionType\">\r\n <PermissionType>Allow</PermissionType>\r\n </Type>\r\n <Inherited>false</Inherited>\r\n <Applicability>\r\n <ToSelf>true</ToSelf>\r\n <ToDescendantObjects>false</ToDescendantObjects>\r\n <ToDescendantContainers>true</ToDescendantContainers>\r\n <ToDirectDescendantsOnly>false</ToDirectDescendantsOnly>\r\n </Applicability>\r\n <Standard>\r\n <GPOGroupedAccessEnum>Edit, delete, modify security</GPOGroupedAccessEnum>\r\n </Standard>\r\n <AccessMask>0</AccessMask>\r\n </TrusteePermissions>\r\n </Permissions>\r\n <AuditingPresent xmlns=\"http://www.microsoft.com/GroupPolicy/Types/Security\">false</AuditingPresent>\r\n </SecurityDescriptor>\r\n <FilterDataAvailable>true</FilterDataAvailable>\r\n <Computer>\r\n <VersionDirectory>3</VersionDirectory>\r\n <VersionSysvol>3</VersionSysvol>\r\n <Enabled>true</Enabled>\r\n <ExtensionData>\r\n <Extension xmlns:q1=\"http://www.microsoft.com/GroupPolicy/Settings/Security\" xsi:type=\"q1:SecuritySettings\">\r\n <q1:Account>\r\n <q1:Name>ClearTextPassword</q1:Name>\r\n <q1:SettingBoolean>false</q1:SettingBoolean>\r\n <q1:Type>Password</q1:Type>\r\n </q1:Account>\r\n <q1:Account>\r\n <q1:Name>LockoutBadCount</q1:Name>\r\n <q1:SettingNumber>0</q1:SettingNumber>\r\n <q1:Type>Account Lockout</q1:Type>\r\n </q1:Account>\r\n <q1:Account>\r\n <q1:Name>MaximumPasswordAge</q1:Name>\r\n <q1:SettingNumber>42</q1:SettingNumber>\r\n <q1:Type>Password</q1:Type>\r\n </q1:Account>\r\n <q1:Account>\r\n <q1:Name>MinimumPasswordAge</q1:Name>\r\n <q1:SettingNumber>1</q1:SettingNumber>\r\n <q1:Type>Password</q1:Type>\r\n </q1:Account>\r\n <q1:Account>\r\n <q1:Name>MinimumPasswordLength</q1:Name>\r\n <q1:SettingNumber>7</q1:SettingNumber>\r\n <q1:Type>Password</q1:Type>\r\n </q1:Account>\r\n <q1:Account>\r\n <q1:Name>PasswordComplexity</q1:Name>\r\n <q1:SettingBoolean>true</q1:SettingBoolean>\r\n <q1:Type>Password</q1:Type>\r\n </q1:Account>\r\n <q1:Account>\r\n <q1:Name>PasswordHistorySize</q1:Name>\r\n <q1:SettingNumber>24</q1:SettingNumber>\r\n <q1:Type>Password</q1:Type>\r\n </q1:Account>\r\n <q1:Account>\r\n <q1:Name>MaxClockSkew</q1:Name>\r\n <q1:SettingNumber>5</q1:SettingNumber>\r\n <q1:Type>Kerberos</q1:Type>\r\n </q1:Account>\r\n <q1:Account>\r\n <q1:Name>MaxRenewAge</q1:Name>\r\n <q1:SettingNumber>7</q1:SettingNumber>\r\n <q1:Type>Kerberos</q1:Type>\r\n </q1:Account>\r\n <q1:Account>\r\n <q1:Name>MaxServiceAge</q1:Name>\r\n <q1:SettingNumber>600</q1:SettingNumber>\r\n <q1:Type>Kerberos</q1:Type>\r\n </q1:Account>\r\n <q1:Account>\r\n <q1:Name>MaxTicketAge</q1:Name>\r\n <q1:SettingNumber>10</q1:SettingNumber>\r\n <q1:Type>Kerberos</q1:Type>\r\n </q1:Account>\r\n <q1:Account>\r\n <q1:Name>TicketValidateClient</q1:Name>\r\n <q1:SettingBoolean>true</q1:SettingBoolean>\r\n <q1:Type>Kerberos</q1:Type>\r\n </q1:Account>\r\n <q1:SecurityOptions>\r\n <q1:KeyName>MACHINE\System\CurrentControlSet\Control\Lsa\NoLMHash</q1:KeyName>\r\n <q1:SettingNumber>1</q1:SettingNumber>\r\n <q1:Display>\r\n <q1:Name>Network security: Do not store LAN Manager hash value on next password change</q1:Name>\r\n <q1:Units />\r\n <q1:DisplayBoolean>true</q1:DisplayBoolean>\r\n </q1:Display>\r\n </q1:SecurityOptions>\r\n <q1:SecurityOptions>\r\n <q1:SystemAccessPolicyName>ForceLogoffWhenHourExpire</q1:SystemAccessPolicyName>\r\n <q1:SettingNumber>0</q1:SettingNumber>\r\n </q1:SecurityOptions>\r\n <q1:SecurityOptions>\r\n <q1:SystemAccessPolicyName>LSAAnonymousNameLookup</q1:SystemAccessPolicyName>\r\n <q1:SettingNumber>0</q1:SettingNumber>\r\n </q1:SecurityOptions>\r\n <q1:Blocked>false</q1:Blocked>\r\n </Extension>\r\n <Name>Security</Name>\r\n </ExtensionData>\r\n <ExtensionData>\r\n <Extension xmlns:q2=\"http://www.microsoft.com/GroupPolicy/Settings/PublicKey\" xsi:type=\"q2:PublicKeySettings\">\r\n <q2:EFSSettings>\r\n <q2:AllowEFS>2</q2:AllowEFS>\r\n <q2:Options>0</q2:Options>\r\n <q2:CacheTimeout>0</q2:CacheTimeout>\r\n <q2:KeyLen>0</q2:KeyLen>\r\n </q2:EFSSettings>\r\n <q2:EFSRecoveryAgent>\r\n <q2:IssuedTo>Administrator</q2:IssuedTo>\r\n <q2:IssuedBy>Administrator</q2:IssuedBy>\r\n <q2:ExpirationDate>2120-09-06T08:45:08Z</q2:ExpirationDate>\r\n <q2:CertificatePurpose>\r\n <q2:Purpose>1.3.6.1.4.1.311.10.3.4.1</q2:Purpose>\r\n </q2:CertificatePurpose>\r\n <q2:Data>0200000001000000CC0000001C0000006C0000000100000000000000000000000000000001000000610031003900650039003000350033002D0036003200650037002D0034003000610061002D0038006300620035002D0065003200300032006500300031003300620033006100310000000000000000004D006900630072006F0073006F0066007400200045006E00680061006E006300650064002000430072007900700074006F0067007200610070006800690063002000500072006F00760069006400650072002000760031002E00300000000000030000000100000014000000B664183C38ED8EBA8CBAD8438B28D73FCC9A86BC2000000001000000850300003082038130820269A00302010202103F608D5FC797B2BF480FC0B8C36D13A2300D06092A864886F70D01010505003050311630140603550403130D41646D696E6973747261746F72310C300A0603550407130345465331283026060355040B131F4546532046696C6520456E6372797074696F6E2043657274696669636174653020170D3230303933303038343530385A180F32313230303930363038343530385A3050311630140603550403130D41646D696E6973747261746F72310C300A0603550407130345465331283026060355040B131F4546532046696C6520456E6372797074696F6E20436572746966696361746530820122300D06092A864886F70D01010105000382010F003082010A0282010100CE0D82D3E5411B73D06675FF6222B598598D70154CEF9D3421378E3C250047342F9CA97FA2BC4B32D5294496910454A1DF19E3E2932BC03E691D0797D5F59184A895A0AF73B49F0DD0417D008AB2F766332D0C31FA004F365E395B7CBC02C41E8B3783709F53BDDE8826047227F6AB54C2EF138D4F85270D74D6CC8B3BFB1ED836161A5A297860BD3EDF50031634985E12D38D4425384F6B6F855C4D64C7CE4E5F7BF6077AB6A9242204CAA6E060DD381EC77DD6FACF7533F1ED49860F2BB6000C9F20F53FEB7D28A6FA4CCCBCC8ECD851BC12EA820B2AE28F2F0A0AA8A5853417A1A31DBBC976B2955874D6E1C61702A97453CAAD283AA1A914D9BE11B2193D0203010001A355305330160603551D25040F300D060B2B0601040182370A030401302E0603551D1104273025A023060A2B060104018237140203A0150C1341646D696E6973747261746F7240544553540030090603551D1304023000300D06092A864886F70D0101050500038201010045ADC5612E87B49FEFAD5A3A84768A495344D5ACC9D34CAA5C635CB42B1FFB76EAA123712BD3FD113F4AC63B01062A22BAE6D6DCF208035DEF8BCE561B035B1C9087614EEF8A193A216C766CD20DC695870FBDA67F6142B7682E962025436F15D75ED4B081B587AFEE93683800BB1DC2533F1D31141D764999300F1F42B1A778A9226D5500746D782B591153F172F1FC21D175154C31BD9F41B97428870E3A3CEC1EBA05624335C43C24D00BBCFD3ECD939D120C02E5C19D0BC04F81922CC8B92E37F907E2984F710F25C6FB6D38BEC874239CD4FFBA76B66893F397B5A82B5069E57B9A82A454B7108F60E5DF697C7ABAAEF7B32DB7BEB449A2CD66132609B3</q2:Data>\r\n </q2:EFSRecoveryAgent>\r\n <q2:RootCertificateSettings>\r\n <q2:AllowNewCAs>true</q2:AllowNewCAs>\r\n <q2:TrustThirdPartyCAs>true</q2:TrustThirdPartyCAs>\r\n <q2:RequireUPNNamingConstraints>false</q2:RequireUPNNamingConstraints>\r\n </q2:RootCertificateSettings>\r\n </Extension>\r\n <Name>Public Key</Name>\r\n </ExtensionData>\r\n <ExtensionData>\r\n <Extension xmlns:q3=\"http://www.microsoft.com/GroupPolicy/Settings/Registry\" xsi:type=\"q3:RegistrySettings\">\r\n <q3:Blocked>false</q3:Blocked>\r\n </Extension>\r\n <Name>Registry</Name>\r\n </ExtensionData>\r\n </Computer>\r\n <User>\r\n <VersionDirectory>0</VersionDirectory>\r\n <VersionSysvol>0</VersionSysvol>\r\n <Enabled>true</Enabled>\r\n </User>\r\n <LinksTo>\r\n <SOMName>test</SOMName>\r\n <SOMPath>test.local</SOMPath>\r\n <Enabled>true</Enabled>\r\n <NoOverride>false</NoOverride>\r\n </LinksTo>\r\n</GPO>"
现在我想读它/想读出一些价值观。例如,如果我想从“MaxServiceAge”(值为 600)中读取值,我该怎么做?
此致, 亨德里克
使用 Xml Linq:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
string xml = File.ReadAllText(FILENAME);
XDocument doc = XDocument.Parse(xml);
List<XElement> xAccounts = doc.Descendants().Where(x => x.Name.LocalName == "Account").ToList();
XNamespace q1 = xAccounts.FirstOrDefault().GetNamespaceOfPrefix("q1");
Dictionary<string, Account> accountDict = xAccounts.Select(x => new Account()
{
Name = (string)x.Element(q1 + "Name"),
Setting = x.Elements().Where(y => y.Name.LocalName.StartsWith("Setting")).Select(y => new KeyValuePair<string,string>(y.Name.LocalName, (string)y)).FirstOrDefault(),
Type = (string)x.Element(q1 + "Type")
})
.GroupBy(x => x.Name, y => y)
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
Account MaxServiceAge = accountDict["MaxServiceAge"];
}
}
public class Account
{
public string Name { get; set;}
public KeyValuePair<string,string> Setting { get; set;}
public string Type { get; set;}
}
}