使用属性xsi:type创建XML节点 [英] Create XML Node with attribute xsi:type

查看:83
本文介绍了使用属性xsi:type创建XML节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在尝试创建一个XML文档。在in中有一个名为Column的元素,其属性为xsi:type =SQLINT。但是,虽然我描述的属性是它唯一的创建类型。我应用的代码在下面



 attribute = xmlDoc.CreateAttribute(  xsi:type); 
attribute.Value = CharTerm;
userNode.Attributes.Append(attribute);

解决方案

您需要在创建属性时指定命名空间:



 attribute = doc.CreateAttribute(  xsi:type  http://www.w3.org/2001/XMLSchema-instance) ; 





但是这会将xmlns声明添加到子节点:

 <   doc  >  
< user xsi:type = CharTerm xmlns:xsi = http://www.w3.org/2001/XMLSchema-instance / >
< / doc >





如果你希望它在你需要明确声明它的根元素上:

  string  xsi = < span class =code-string>  http://www.w3.org/2001/XMLSchema-instance; 
var xmlns = doc.CreateAttribute( xmlns:xsi http://www.w3.org/2000/xmlns/< /跨度>);
xmlns.Value = xsi;
doc.DocumentElement.Attributes.Append(xmlns);

attribute = doc.CreateAttribute( xsi:type,xsi) ;





然后你得到这个:

 <   doc     xmlns:xsi   =  http://www.w3.org/2001/XMLSchema-instance >  
< user xsi:type = CharTerm / >
< / doc >


Hi,

I am trying to create an XML document . With in there is a element called Column with property xsi:type="SQLINT". But eventhough i am describing the attribute its only creating type.Code i applied is below

attribute = xmlDoc.CreateAttribute("xsi:type");
attribute.Value = "CharTerm";
userNode.Attributes.Append(attribute);

解决方案

You need to specify the namespace when you create the attribute:

attribute = doc.CreateAttribute("xsi:type", "http://www.w3.org/2001/XMLSchema-instance");



However this will add the xmlns declaration to the child node:

<doc>
  <user xsi:type="CharTerm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
</doc>



If you want it to be on the root element you need to declare it explicitly:

string xsi = "http://www.w3.org/2001/XMLSchema-instance";
var xmlns = doc.CreateAttribute("xmlns:xsi", "http://www.w3.org/2000/xmlns/");
xmlns.Value = xsi;
doc.DocumentElement.Attributes.Append(xmlns);

attribute = doc.CreateAttribute("xsi:type", xsi);



Then you get this:

<doc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <user xsi:type="CharTerm" />
</doc>


这篇关于使用属性xsi:type创建XML节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆