我正在嘗試處理 Windows Autounattend.xml 檔案的讀取和寫入,以便創建和修改它們。我無法獲取xmlns:wcm并且xmlns:xsi屬性被正確編組和解組。
<?xml version="1.0" encoding="utf-8"?>
<unattend xmlns="urn:schemas-microsoft-com:unattend">
<settings pass="windowsPE">
<component name="Microsoft-Windows-International-Core-WinPE" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS"
xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SetupUILanguage>
<UILanguage>en-US</UILanguage>
</SetupUILanguage>
<InputLocale>0409:00000409</InputLocale>
<SystemLocale>en-US</SystemLocale>
<UILanguage>en-US</UILanguage>
<UILanguageFallback>en-US</UILanguageFallback>
<UserLocale>en-US</UserLocale>
</component>
<component name="Microsoft-Windows-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS"
xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<DiskConfiguration>
<Disk wcm:action="add">
<DiskID>0</DiskID>
<WillWipeDisk>true</WillWipeDisk>
<CreatePartitions>
<!-- Windows RE Tools partition -->
<CreatePartition wcm:action="add">
<Order>1</Order>
<Type>Primary</Type>
<Size>300</Size>
</CreatePartition>
<!-- System partition (ESP) -->
<CreatePartition wcm:action="add">
<Order>2</Order>
<Type>EFI</Type>
<Size>100</Size>
</CreatePartition>
<!-- Microsoft reserved partition (MSR) -->
<CreatePartition wcm:action="add">
<Order>3</Order>
<Type>MSR</Type>
<Size>128</Size>
</CreatePartition>
<!-- Windows partition -->
<CreatePartition wcm:action="add">
<Order>4</Order>
<Type>Primary</Type>
<Extend>true</Extend>
</CreatePartition>
</CreatePartitions>
<ModifyPartitions>
<ModifyPartition wcm:action="add">
<Order>1</Order>
<PartitionID>1</PartitionID>
<Label>WINRE</Label>
<Format>NTFS</Format>
<TypeID>DE94BBA4-06D1-4D40-A16A-BFD50179D6AC</TypeID>
</ModifyPartition>
<ModifyPartition wcm:action="add">
<Order>2</Order>
<PartitionID>2</PartitionID>
<Label>System</Label>
<Format>FAT32</Format>
</ModifyPartition>
<ModifyPartition wcm:action="add">
<Order>3</Order>
<PartitionID>3</PartitionID>
</ModifyPartition>
<ModifyPartition wcm:action="add">
<Order>4</Order>
<PartitionID>4</PartitionID>
<Label>Windows</Label>
<Letter>C</Letter>
<Format>NTFS</Format>
</ModifyPartition>
</ModifyPartitions>
</Disk>
</DiskConfiguration>
<ImageInstall>
<OSImage>
<InstallTo>
<DiskID>0</DiskID>
<PartitionID>4</PartitionID>
</InstallTo>
<InstallToAvailablePartition>false</InstallToAvailablePartition>
</OSImage>
</ImageInstall>
<UserData>
<ProductKey>
<Key></Key>
<WillShowUI>Never</WillShowUI>
</ProductKey>
<AcceptEula>true</AcceptEula>
<FullName>admin</FullName>
<Organization></Organization>
</UserData>
</component>
</settings>
<settings pass="offlineServicing">
<component name="Microsoft-Windows-LUA-Settings" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS"
xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<EnableLUA>false</EnableLUA>
</component>
</settings>
<settings pass="generalize">
<component name="Microsoft-Windows-Security-SPP" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS"
xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SkipRearm>1</SkipRearm>
</component>
</settings>
<settings pass="specialize">
<component name="Microsoft-Windows-International-Core" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS"
xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<InputLocale>0409:00000409</InputLocale>
<SystemLocale>en-US</SystemLocale>
<UILanguage>en-US</UILanguage>
<UILanguageFallback>en-US</UILanguageFallback>
<UserLocale>en-US</UserLocale>
</component>
<component name="Microsoft-Windows-Security-SPP-UX" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS"
xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SkipAutoActivation>true</SkipAutoActivation>
</component>
<component name="Microsoft-Windows-SQMApi" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS"
xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<CEIPEnabled>0</CEIPEnabled>
</component>
<component name="Microsoft-Windows-Shell-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS"
xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ComputerName>-PC</ComputerName>
<ProductKey></ProductKey>
</component>
</settings>
<settings pass="oobeSystem">
<component name="Microsoft-Windows-Shell-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS"
xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<AutoLogon>
<Password>
<Value></Value>
<PlainText>true</PlainText>
</Password>
<Enabled>true</Enabled>
<Username>admin</Username>
</AutoLogon>
<OOBE>
<HideEULAPage>true</HideEULAPage>
<HideOEMRegistrationScreen>true</HideOEMRegistrationScreen>
<HideOnlineAccountScreens>true</HideOnlineAccountScreens>
<HideWirelessSetupInOOBE>true</HideWirelessSetupInOOBE>
<NetworkLocation>Home</NetworkLocation>
<SkipUserOOBE>true</SkipUserOOBE>
<SkipMachineOOBE>true</SkipMachineOOBE>
<ProtectYourPC>1</ProtectYourPC>
</OOBE>
<UserAccounts>
<LocalAccounts>
<LocalAccount wcm:action="add">
<Password>
<Value></Value>
<PlainText>true</PlainText>
</Password>
<Description></Description>
<DisplayName>admin</DisplayName>
<Group>Administrators</Group>
<Name>admin</Name>
</LocalAccount>
</LocalAccounts>
</UserAccounts>
<RegisteredOrganization></RegisteredOrganization>
<RegisteredOwner>admin</RegisteredOwner>
<DisableAutoDaylightTimeSet>false</DisableAutoDaylightTimeSet>
<FirstLogonCommands>
<SynchronousCommand wcm:action="add">
<Description>Control Panel View</Description>
<Order>1</Order>
<CommandLine>reg add "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ControlPanel" /v StartupPage /t REG_DWORD /d 1 /f</CommandLine>
<RequiresUserInput>true</RequiresUserInput>
</SynchronousCommand>
<SynchronousCommand wcm:action="add">
<Order>2</Order>
<Description>Control Panel Icon Size</Description>
<RequiresUserInput>false</RequiresUserInput>
<CommandLine>reg add "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ControlPanel" /v AllItemsIconView /t REG_DWORD /d 0 /f</CommandLine>
</SynchronousCommand>
<SynchronousCommand wcm:action="add">
<Order>3</Order>
<RequiresUserInput>false</RequiresUserInput>
<CommandLine>cmd /C wmic useraccount where name="admin" set PasswordExpires=false</CommandLine>
<Description>Password Never Expires</Description>
</SynchronousCommand>
</FirstLogonCommands>
<TimeZone>Central Standard Time</TimeZone>
</component>
</settings>
</unattend>
我嘗試為此使用xml.Name,xml.Attr和 string ,但沒有成功
const (
// A generic XML header suitable for use with the output of Marshal.
Header = `<?xml version="1.0" encoding="UTF-8"?>` "\n"
WCM = `http://schemas.microsoft.com/WMIConfig/2002/State`
XSI = `http://www.w3.org/2001/XMLSchema-instance`
)
type UserData struct {
AcceptEula bool `xml:"AcceptEula"`
FullName string `xml:"FullName"`
Organization string `xml:"Organization"`
ProductKey struct {
Key string `xml:"Key"`
WillShowUI string `xml:"WillShowUI"`
} `xml:"ProductKey"`
}
type OSImage struct {
XMLName xml.Name `xml:"OSImage"`
InstallToAvailablePartition bool `xml:"InstallToAvailablePartition"`
InstallToDiskID uint32 `xml:"InstallTo>DiskID"`
InstallToPatitionID uint32 `xml:"InstallTo>ParitionID"`
}
type ImageInstall struct {
XMLName xml.Name `xml:"ImageInstall"`
OSImage OSImage `xml:"OSImage"`
}
type CreatePartition struct {
XMLName xml.Name `xml:"CreatePartition"`
Order uint32 `xml:"Order"`
Type string `xml:"Type"`
Size uint64 `xml:"Size,omitempty"`
Extend bool `xml:"Extend,omitempty"`
Action xml.Attr `xml:"action,attr"`
}
type ModifyPartition struct {
XMLName xml.Name `xml:"ModifyPartition"`
Order uint32 `xml:"Order"`
PartitionID uint32 `xml:"PartitionID"`
Label string `xml:"Label,omitempty"`
Format string `xml:"Format,omitempty"`
TypeID string `xml:"TypeID,omitempty"`
Letter string `xml:"Letter,omitempty"`
Action xml.Attr `xml:"action,attr"`
}
type Disk struct {
XMLName xml.Name `xml:"Disk"`
Action xml.Attr `xml:"action,attr"`
DiskID uint32 `xml:"DiskID"`
WillWipeDisk bool `xml:"WillWipeDisk"`
CreatePartitions []*CreatePartition `xml:"CreatePartitions>."`
ModifyPartitions []*ModifyPartition `xml:"ModifyPartitions>."`
}
type DiskConfiguration struct {
XMLName xml.Name `xml:"DiskConfiguration"`
Disks []*Disk `xml:"Disk"`
}
type SetupUILanguage struct {
XMLName xml.Name `xml:"SetupUILanguage"`
UILanguage *UILanguage `xml:"UILanguage"`
}
type UserLocale struct {
XMLName xml.Name `xml:"UserLocale"`
Value string `xml:",chardata"`
}
type InputLocale struct {
XMLName xml.Name `xml:"InputLocale"`
Value string `xml:",chardata"`
}
type SystemLocale struct {
XMLName xml.Name `xml:"SystemLocale"`
Value string `xml:",chardata"`
}
type UILanguage struct {
XMLName xml.Name `xml:"UILanguage"`
Value string `xml:",chardata"`
}
type UILanguageFallback struct {
XMLName xml.Name `xml:"UILanguageFallback"`
Value string `xml:",chardata"`
}
type EnableLUA struct {
XMLName xml.Name `xml:"EnableLUA"`
Value bool `xml:",chardata"`
}
type SkipRearm struct {
XMLName xml.Name `xml:"SkipRearm"`
Value int32 `xml:",chardata"`
}
type SkipAutoActivation struct {
XMLName xml.Name `xml:"SkipAutoActivation"`
Value bool `xml:",chardata"`
}
type CEIPEnabled struct {
XMLName xml.Name `xml:"CEIPEnabled"`
Value int32 `xml:",chardata"`
}
type ComputerName struct {
XMLName xml.Name `xml:"ComputerName"`
Value string `xml:",chardata"`
}
type ProductKey struct {
XMLName xml.Name `xml:"ProductKey"`
Value string `xml:",chardata"`
}
type Password struct {
XMLName xml.Name `xml:"Password"`
Value string `xml:"Value"`
PlainText bool `xml:"PlainText"`
}
type AutoLogon struct {
XMLName xml.Name `xml:"AutoLogon"`
Enabled bool `xml:"Enabled"`
Username string `xml:"Username"`
Password Password `xml:"Password"`
}
type OOBE struct {
XMLName xml.Name `xml:"OOBE"`
HideEULAPage bool `xml:"HideEULAPage,omitempty"`
HideOEMRegistrationScreen bool `xml:"HideOEMRegistrationScreen,omitempty"`
HideOnlineAccountScreens bool `xml:"HideOnlineAccountScreens,omitempty"`
HideWirelessSetupInOOBE bool `xml:"HideWirelessSetupInOOBE,omitempty"`
NetworkLocation string `xml:"NetworkLocation,omitempty"`
SkipUserOOBE bool `xml:"SkipUserOOBE,omitempty"`
SkipMachineOOBE bool `xml:"SkipMachineOOBE,omitempty"`
ProtectYourPC int32 `xml:"ProtectYourPC,omitempty"`
}
type LocalAccount struct {
XMLName xml.Name `xml:"LocalAccount"`
Action xml.Attr `xml:"action,attr"`
Password Password `xml:"Password"`
Description string `xml:"Description"`
DisplayName string `xml:"DisplayName"`
Group string `xml:"Group"`
Name string `xml:"Name"`
}
type LocalAccounts struct {
XMLName xml.Name
LocalAccount []*LocalAccount `xml:"LocalAccount"`
}
type UserAccounts struct {
XMLName xml.Name `xml:"UserAccounts"`
LocalAccounts LocalAccounts `xml:"LocalAccounts"`
}
type RegisteredOrganization struct {
XMLName xml.Name `xml:"RegisteredOrganization"`
Value string `xml:",chardata"`
}
type RegisteredOwner struct {
XMLName xml.Name `xml:"RegisteredOwner"`
Value string `xml:",chardata"`
}
type DisableAutoDaylightTimeSet struct {
XMLName xml.Name `xml:"DisableAutoDaylightTimeSet"`
Value bool `xml:",chardata"`
}
type CommandLine struct {
XMLName xml.Name `xml:"CommandLine"`
Value string `xml:",innerxml"`
}
type SynchronousCommand struct {
XMLName xml.Name `xml:"SynchronousCommand"`
Order uint32 `xml:"Order"`
Description string `xml:"Description"`
RequiresUserInput bool `xml:"RequiresUserInput"`
CommandLine CommandLine `xml:"CommandLine"`
}
type FirstLogonCommands struct {
XMLName xml.Name `xml:"FirstLogonCommands"`
SynchronousCommand []*SynchronousCommand `xml:"SynchronousCommand"`
}
type TimeZone struct {
XMLName xml.Name `xml:"TimeZone"`
Value string `xml:",chardata"`
}
type Component struct {
XMLName xml.Name `xml:"component"`
Name string `xml:"name,attr"`
ProcessorArchitecture string `xml:"processorArchitecture,attr"`
PublicKeyToken string `xml:"publicKeyToken,attr"`
Language string `xml:"language,attr"`
VersionScope string `xml:"versionScope,attr"`
// optional compontents
SetupUILanguage *SetupUILanguage `xml:"SetupUILanguage"`
UserLocale *UserLocale `xml:"UserLocale"`
InputLocale *InputLocale `xml:"InputLocale"`
SystemLocale *SystemLocale `xml:"SystemLocale"`
UILanguage *UILanguage `xml:"UILanguage"`
UILanguageFallback *UILanguageFallback `xml:"UILanguageFallback"`
DiskConfiguration *DiskConfiguration `xml:"DiskConfiguration,omitempty"`
ImageInstall *ImageInstall `xml:"ImageInstall,omitempty"`
UserData *UserData `xml:"UserData,omitempty"`
EnableLUA *EnableLUA `xml:"EnableLUA,omitempty"`
SkipRearm *SkipRearm `xml:"SkipRearm,omitempty"`
ProductKey *ProductKey `xml:"ProductKey,omitempty"`
ComputerName *ComputerName `xml:"ComputerName,omitempty"`
SkipAutoActivation *SkipAutoActivation `xml:"SkipAutoActivation,omitempty"`
CEIPEnabled *CEIPEnabled `xml:"CEIPEnabled,omitempty"`
AutoLogon *AutoLogon `xml:"AutoLogon,omitempty"`
OOBE *OOBE `xml:"OOBE,omitempty"`
UserAccounts *UserAccounts `xml:"UserAccounts"`
RegisteredOrganization *RegisteredOrganization `xml:"RegisteredOrganization,omitempty"`
RegisteredOwner *RegisteredOwner `xml:"RegisteredOwner,omitempty"`
DisableAutoDaylightTimeSet *DisableAutoDaylightTimeSet `xml:"DisableAutoDaylightTimeSet,omitempty"`
FirstLogonCommands *FirstLogonCommands `xml:"FirstLogonCommands,omitempty"`
TimeZone *TimeZone `xml:"TimeZone,omitempty"`
}
type Settings struct {
XMLName xml.Name `xml:"settings"`
Pass string `xml:"pass,attr"`
Components []*Component `xml:"component"`
}
type Unattend struct {
XMLName xml.Name `xml:"unattend"`
XMLNS xml.Attr `xml:"xmlns,attr"`
Settings []*Settings `xml:"settings"`
}
uj5u.com熱心網友回復:
xmlns:wcm并且xmlns:xsi不是真正的屬性,它們是經過特殊處理的。您的 XML 既沒有元素也沒有來自wcm和xsi模式的屬性,它們被洗掉了。為什么你的代碼中需要它們?
仍然如果您真的需要它們,您可以將以下欄位添加到Component結構中:
Attr []xml.Attr `xml:",any,attr"`
xml.Unmarshal檔案說:
如果 XML 元素有一個屬性沒有被前面的規則處理,并且結構有一個包含“,any,attr”的關聯標簽的欄位,Unmarshal 將屬性值記錄在第一個這樣的欄位中。
這是一個例子:https ://go.dev/play/p/tGDh5Ay1kZW
func main() {
var u Unattend
err := xml.Unmarshal([]byte(document), &u)
if err != nil {
log.Fatal(err)
}
for _, a := range u.Settings[0].Components[0].Attr {
fmt.Printf("Unmatched attributes: %s:%s = %s\n", a.Name.Space, a.Name.Local, a.Value)
}
}
輸出:
Unmatched attributes: xmlns:wcm = http://schemas.microsoft.com/WMIConfig/2002/State
Unmatched attributes: xmlns:xsi = http://www.w3.org/2001/XMLSchema-instance
注意 1. 這個解決方案不太好:它只處理額外的屬性<component/>。但是xmlns:XXX屬性可以出現在任何元素中。在一般情況下,您應該Attr在資料模型的所有結構中添加陣列。
注2.該屬性xmlns:wcm不xmlns:xsi承擔任何業務邏輯。它們沒有提供有關設定和組件的資訊。你真的需要它們嗎?
注 3. 絕對不需要命名這些屬性xmlns:wcm和xmlns:xsi. 這只是一種常見的做法。XML 檔案可以命名它們xmlns:foo并且xmlns:bar- 它是完全有效的。即使是價值觀"http://schemas.microsoft.com/WMIConfig/2002/State","http://www.w3.org/2001/XMLSchema-instance"也沒有任何意義。它們可以是任何字串,唯一的要求是匹配URI 格式。例如urn:microsoft:wmi-config:2002:state,與http://schemas.microsoft.com/WMIConfig/2002/State
決議這些屬性Component是非常具體的,并且可能會在其他有效的 XML 檔案中失敗,這些檔案在其他元素中或使用其他后綴或值宣告這些屬性。
更新
編組作業不如預期。xml.Marshal專門處理xmlns命名空間并轉換Attr為命名空間xmlns:wcm并轉換為xmlns:xsi命名空間_xmlns
<component name="Microsoft-Windows-Security-SPP" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:_xmlns="xmlns" _xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" _xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
...
</component>
解決方法是對屬性使用特殊型別:
type XMLNSAttr struct {
Name xml.Name
Value string
}
func (a XMLNSAttr) Label() string {
if a.Name.Space == "" {
return a.Name.Local
}
if a.Name.Local == "" {
return a.Name.Space
}
return a.Name.Space ":" a.Name.Local
}
func (a *XMLNSAttr) UnmarshalXMLAttr(attr xml.Attr) error {
a.Name = attr.Name
a.Value = attr.Value
return nil
}
func (a XMLNSAttr) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
return xml.Attr{
Name: xml.Name{
Space: "",
Local: a.Label(),
},
Value: a.Value,
}, nil
}
這種型別通過構造一個將命名空間嵌入到本地名稱中的屬性來解決問題。場
Attr []xml.Attr `xml:",any,attr"`
正確解碼/編碼xmlns:xxx屬性
示例:https ://go.dev/play/p/VDofREl5nf1
輸出:
Unmatched attributes: xmlns:wcm = http://schemas.microsoft.com/WMIConfig/2002/State
Unmatched attributes: xmlns:xsi = http://www.w3.org/2001/XMLSchema-instance
<unattend>
<settings pass="windowsPE">
<component name="Microsoft-Windows-International-Core-WinPE" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
...
</component>
...
</settings>
</unattend>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/526307.html
標籤:xml去编组解组
