第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

正確編組和解組包含 xmlns:wcm 和 xmlns:xsi 的 XML

正確編組和解組包含 xmlns:wcm 和 xmlns:xsi 的 XML

Go
炎炎設(shè)計 2023-03-07 17:31:50
我正在嘗試處理 Windows Autounattend.xml 文件的讀寫,以便創(chuàng)建和修改它們。我無法獲取xmlns:wcm和xmlns:xsi正確編組和解組屬性。我正在嘗試處理 Windows Autounattend.xml 文件的讀寫,以便創(chuàng)建和修改它們。我無法獲取xmlns:wcm和xmlns:xsi正確編組和解組屬性。
查看完整描述

1 回答

?
眼眸繁星

TA貢獻1873條經(jīng)驗 獲得超9個贊

xmlns:wcmxmlns:xsi不是真正的屬性,它們是經(jīng)過特殊處理的。您的 XML 既沒有元素也沒有來自wcmxsi模式的屬性,它們將被刪除。為什么你的代碼中需要它們?

不過,如果您真的非常需要它們,您可以將以下字段添加到結(jié)構(gòu)中Component

    Attr                  []xml.Attr `xml:",any,attr"`

xml.Unmarshal文件說

如果 XML 元素具有先前規(guī)則未處理的屬性,并且結(jié)構(gòu)具有包含“,any,attr”的關(guān)聯(lián)標記的字段,則 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屬性可以出現(xiàn)在任何元素中。在一般情況下,您應(yīng)該Attr在數(shù)據(jù)模型的所有結(jié)構(gòu)中添加數(shù)組。

注2. 屬性xmlns:wcmxmlns:xsi不承擔(dān)任何業(yè)務(wù)邏輯。他們沒有提供有關(guān)設(shè)置和組件的信息。你真的需要它們嗎?

注 3. 絕對沒有要求命名這些屬性xmlns:wcmxmlns: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:statehttp://schemas.microsoft.com/WMIConfig/2002/State

解析這些屬性Component非常具體,可能會在其他有效的 XML 文檔上失敗,這些文檔在其他元素中或使用其他后綴或值聲明這些屬性。

更新

編組工作不如預(yù)期。xml.Marshal專門處理xmlns名稱空間并轉(zhuǎn)換Attr為名稱空間xmlns:wcm并轉(zhuǎn)換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

這種類型通過構(gòu)造一個將命名空間嵌入到本地名稱中的屬性來達到目的。場

    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>


查看完整回答
反對 回復(fù) 2023-03-07
  • 1 回答
  • 0 關(guān)注
  • 210 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號