我需要能够在 xml 元素中定义带有前缀的属性。
例如...
<nc:Person s:id="ID_Person_01"></nc:Person>
为了做到这一点,我认为以下内容会起作用。
XmlElement TempElement = XmlDocToRef.CreateElement("nc:Person", "http://niem.gov/niem/niem-core/2.0");
TempElement.SetAttribute("s:id", "http://niem.gov/niem/structures/2.0", "ID_Person_01");
不幸的是, XmlElement.SetAttribute(string, string, string) 似乎不支持解析前缀,因为我收到以下错误。
名称中不能包含“:”字符(十六进制值 0x3A)。
如何定义带有前缀的属性?
如果您已经在根节点中声明了命名空间,则只需更改SetAttribute
调用以使用无前缀的属性名称。因此,如果您的根节点定义了这样的命名空间:
<People xmlns:s='http://niem.gov/niem/structures/2.0'>
您可以执行此操作,该属性将拾取您已经建立的前缀:
// no prefix on the first argument - it will be rendered as
// s:id='ID_Person_01'
TempElement.SetAttribute("id", "http://niem.gov/niem/structures/2.0", "ID_Person_01");
如果您尚未声明名称空间(及其前缀),则三字符串XmlDocument.CreateAttribute http://msdn.microsoft.com/en-us/library/zs2zzesf.aspx超载会为你做到这一点:
// Adds the declaration to your root node
var attribute = xmlDocToRef.CreateAttribute("s", "id", "http://niem.gov/niem/structures/2.0");
attribute.InnerText = "ID_Person_01"
TempElement.SetAttributeNode(attribute);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)