ASP.net创造和的XDocument一个XLink节点 [英] ASP.net Creating an XLink node in and XDocument

查看:112
本文介绍了ASP.net创造和的XDocument一个XLink节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个新的XLink节点编程方式添加到一个XDocument,但净似乎被创建为雾化命名空间和名字,我无法找到任何code到XLink的节点添加到XML?

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?

我的code是这样的:

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);

不幸的是这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>

但我需要的节点到这个样子,以便它通过XML验证:

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>

有人能帮忙吗?我不想下井与string.replace()的路线,因为它似乎这一定是可能的另一种方式?

Can anybody help? I don't want to go down the String.Replace() route as it seems that this must be possible another way?

感谢

瑞安

推荐答案

我会做,因为:

XNamespace ns = "http://www.w3.org/1999/xlink";

XElement newNode = new XElement("large_images",
    new XAttribute(XNamespace.Xmlns + "xlink", ns),
    new XAttribute(ns + "type", "simple),
    new XAttribute(ns + "href", "tcm:5-550"),
    new XAttribute(ns + "title", "of1_454x340.jpg"));

这产生的XML:

<large_images xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple"
    xlink:href="tcm:5-550" xlink:title="of1_454x340.jpg" />

这篇关于ASP.net创造和的XDocument一个XLink节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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