0

我正在尝试使用 XmlWriter 将以下属性添加到节点,但似乎没有任何效果,有人有什么想法吗?

<news:news>

我正在努力实现以下目标:

 <url>
    <loc>http://www.example.org/business/article55.html</loc>
    <news:news>
      <news:publication>
        <news:name>The Example Times</news:name>
        <news:language>en</news:language>
      </news:publication>
      <news:access>Subscription</news:access>
      <news:genres>PressRelease, Blog</news:genres>
      <news:publication_date>2008-12-23</news:publication_date>
      <news:title>Companies A, B in Merger Talks</news:title>
      <news:keywords>business, merger, acquisition, A, B</news:keywords>
      <news:stock_tickers>NASDAQ:A, NASDAQ:B</news:stock_tickers>
    </news:news>
  </url>

谢谢

4

2 回答 2

1

查看以下链接,了解如何使用 XmlWriter 处理命名空间

XmlWriter 中的命名空间处理

您可以使用 WriteAttributeString 方法手动写出命名空间声明,然后使用WriteStartElement(String, String)重载将未来元素与该命名空间相关联,如下所示

writer.WriteStartElement("root");
writer.WriteAttributeString("xmlns", "x", null, "urn:1");
writer.WriteStartElement("item", "urn:1");
writer.WriteEndElement();
writer.WriteStartElement("item", "urn:1");
writer.WriteEndElement();
writer.WriteEndElement();
于 2012-11-30T15:36:37.367 回答
0
    using System;
    using System.Xml;

    class Program
    {
        static void Main(string[] args)
        {
            XmlWriter writer = new XmlTextWriter(Console.Out);
            writer.WriteStartElement("root");
            writer.WriteAttributeString("news", "http://www.stackoverflow.com");
            writer.WriteStartElement("news:news");
            writer.WriteStartElement("news:publication");
            writer.WriteElementString("news:name", "The Example Times");
            writer.WriteElementString("news:language", "en");
            // etc
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndElement();
            Console.ReadKey(true);
        }
    }
于 2012-11-30T15:42:50.630 回答