0

我正在以编程方式创建一个节点,但是其中一个属性的出现与我在代码中指定的不同:

                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" />
4

2 回答 2

0

您可以直接为属性设置前缀,而不是尝试创建与前缀内联的属性。

XmlAttribute xRefADLCP = docXMLFile.CreateAttribute("scormtype");
// ...
xRefADLCP.Prefix = "adlcp";
xRefADLCP.Value = "sco";
于 2012-06-04T16:50:59.940 回答
0

这可能是此覆盖CreateAttribute与保存文档相结合的预期行为:

NamespaceURI 保持为空,除非前缀是可识别的内置前缀,例如 xmlns。在这种情况下, NamespaceURI 的值为http://www.w3.org/2000/xmlns/

使用另一个覆盖XmlDocument.CreateAttribute来指定命名空间和前缀:

XmlAttribute xRefADLCP = docXMLFile.CreateAttribute(
     "adlcp","scormtype", "correct-namespace-here");
于 2012-06-04T16:57:22.270 回答