簡體   English   中英

C#XML屬性創建不正確

[英]C# XML attribute being created improperly

我正在以編程方式創建一個節點,但是屬性之一的出現與我在代碼中指定的不同:

                XmlNode xResource = docXMLFile.CreateNode(XmlNodeType.Element, "resource", docXMLFile.DocumentElement.NamespaceURI);
                XmlAttribute xRefIdentifier = docXMLFile.CreateAttribute("identifier");
                XmlAttribute xRefADLCP = docXMLFile.CreateAttribute("adlcp:scormtype");
                XmlAttribute xRefHREF = docXMLFile.CreateAttribute("href");
                XmlAttribute xRefType = docXMLFile.CreateAttribute("type");
                xRefIdentifier.Value = "RES-" + strRes;
                xRefADLCP.Value = "sco";
                xRefHREF.Value = dataRow["launch_url"].ToString().ToLower();
                xRefType.Value = "webcontent";

                xResource.Attributes.Append(xRefIdentifier);
                xResource.Attributes.Append(xRefADLCP);
                xResource.Attributes.Append(xRefHREF);
                xResource.Attributes.Append(xRefType);

這樣最終創建了如下一行。 請注意,“ adlcp:scormtype”已變形為“ scormtype”,這不是我指定的。 任何想法如何使其顯示我在CreateAttribute中放置的內容?

 <resource identifier="RES-CDA68F64B849460B93BF2840A9487358" scormtype="sco" href="start.html" type="webcontent" />

您可以直接為屬性設置Prefix ,而不是嘗試使用前綴內聯創建屬性。

XmlAttribute xRefADLCP = docXMLFile.CreateAttribute("scormtype");
// ...
xRefADLCP.Prefix = "adlcp";
xRefADLCP.Value = "sco";

這可能是此重寫CreateAttribute結合保存文檔的預期行為:

除非前綴是公認的內置前綴(例如xmlns),否則NamespaceURI保持為空。 在這種情況下,NamespaceURI的值為http://www.w3.org/2000/xmlns/

使用另一個重寫XmlDocument.CreateAttribute指定名稱空間和前綴:

XmlAttribute xRefADLCP = docXMLFile.CreateAttribute(
     "adlcp","scormtype", "correct-namespace-here");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM