ASP.net 在 XDocument 中创建 XLink 节点
我正在尝试以编程方式将新的 XLink 节点添加到 XDocument,但 .Net 似乎将它们创建为原子化命名空间和名称,并且我找不到任何代码来将 XLink 节点添加到 XML?
我的代码如下所示:
//read in the current XML content
XDocument content = XDocument.Parse(xmlContent);
//add a new node called large images
XElement newNode = new XElement("large_images", "");
newNode.SetAttributeValue("{xmlns}xlink", "http://www.w3.org/1999/xlink");
newNode.SetAttributeValue("{xlink}type", "simple");
newNode.SetAttributeValue("{xlink}href", "tcm:5-550");
newNode.SetAttributeValue("{xlink}title", "of1_454x340.jpg");
content.Add(newNode);
不幸的是,这个 newNode 的结果如下:
<large_images p1:xlink="http://www.w3.org/1999/xlink" p2:type="simple" p2:href="tcm:5-550" p2:title="of1_454x340.jpg" xmlns:p2="xlink" xmlns:p1="xmlns"></large_images>
但我需要节点看起来像这样才能通过 XML 验证:
<large_images xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="tcm:5-550" xlink:title="of1_454x340.jpg"></large_images>
有人能帮忙吗?我不想走 String.Replace() 路线,因为这似乎必须有另一种方式?
谢谢瑞安
I am trying to programmatically add a new XLink node to an XDocument but .Net seems to be creating them as atomized namespaces and names and I can't find any code to add XLink nodes to XML?
My code looks like this:
//read in the current XML content
XDocument content = XDocument.Parse(xmlContent);
//add a new node called large images
XElement newNode = new XElement("large_images", "");
newNode.SetAttributeValue("{xmlns}xlink", "http://www.w3.org/1999/xlink");
newNode.SetAttributeValue("{xlink}type", "simple");
newNode.SetAttributeValue("{xlink}href", "tcm:5-550");
newNode.SetAttributeValue("{xlink}title", "of1_454x340.jpg");
content.Add(newNode);
Unfortunately this newNode comes out like this:
<large_images p1:xlink="http://www.w3.org/1999/xlink" p2:type="simple" p2:href="tcm:5-550" p2:title="of1_454x340.jpg" xmlns:p2="xlink" xmlns:p1="xmlns"></large_images>
But I need the node to look like this in order for it to pass the XML validation:
<large_images xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="tcm:5-550" xlink:title="of1_454x340.jpg"></large_images>
Can anybody help? I don't want to go down the String.Replace() route as it seems that this must be possible another way?
Thanks
Ryan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会这样做:
这会生成以下 XML:
I'd do that as:
This produces XML of: