ASP.net 在 XDocument 中创建 XLink 节点

发布于 2024-10-17 00:43:34 字数 1114 浏览 1 评论 0原文

我正在尝试以编程方式将新的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

咋地 2024-10-24 00:43:34

我会这样做:

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

I'd do that as:

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

This produces XML of:

<large_images xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple"
    xlink:href="tcm:5-550" xlink:title="of1_454x340.jpg" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文