【问题标题】:How to write CData of XML which contain inner nodes in C#?如何在 C# 中编写包含内部节点的 XML 的 CData?
【发布时间】:2019-12-09 05:55:14
【问题描述】:

我有以下 xml 文档

<Node id="1" name="name1" autoplayTrailer="false" visible="true" bgimages="false">
      <Text>
        <title id="2"  type="text" hideineditor="false" visible="true"><![CDATA[Link]]></title>
        <sections id="3"  default="consideration">         
          <songs id="4"  type="text" hideineditor="false" enabled="true" visible="true">
            <![CDATA[
              <div class="ThumbContainer">
                Some text here
              </div>
            ]]>         
            <songsTitle><![CDATA[sometexthtml here]]></songsTitle>
          </songs>
           </sections>
     </Text>
</Node>

我想一一读取content/node,修改CDATA的内容并将xml写入光盘。

问题是我无法为 &lt;songs&gt; 节点编写 CData,因为它在 &lt;songTitle&gt; 节点内有另一个节点,而没有关闭 &lt;/song&gt; 节点是否可以用 CData 编写节点,后面有另一个节点CData 内容?

【问题讨论】:

  • 您使用的是什么 API? LINQ 到 XML?较旧的XmlDocumentXmlSerializer?使用XmlReader 直接阅读(或希望不是)?
  • XmlTextWriter 写入元素..和 StreamReader 读取 xmlstring
  • 这是您正在使用的非常低级的 API。你能分享一个你迄今为止拥有的代码的最小示例,以及你遇到的问题吗?顺便说一句,只要您可以将整个 XML 加载到内存中,使用 LINQ to XML 可能会更容易。

标签: c# xml xml-parsing writexml


【解决方案1】:

试试 xml linq:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            foreach (XElement node in doc.Descendants("Node"))
            {
                XElement songs = node.Descendants("songs").FirstOrDefault();
                XCData child = (XCData)songs.FirstNode;
                string childStr = child.ToString();
                childStr = childStr.Replace("Some text here", "Some text there");
                child.ReplaceWith(childStr);
            }
        }
    }
}

【讨论】:

    【解决方案2】:

    这是您要创建的输出吗?

    此示例使用 XmlTextWriter API 输出“歌曲”元素下的 CData 和“其他元素”。

    using System;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml;
    
    namespace WriteCData
    {
    class Program
    {
        static void Main(string[] args)
        {
            // some output to write to
            XmlTextWriter xtw = new XmlTextWriter(@"c:\temp\CDataTest.xml", null);
    
            // start at 'songs' element
            xtw.WriteStartElement("songs");
            xtw.WriteCData("<div class='ThumbContainer'>Some text here</div>");
            xtw.WriteStartElement("songsTitle");
            xtw.WriteCData("sometexthtml here");
            xtw.WriteEndElement();      // end "songTitle"
            xtw.WriteEndElement();      // end "songs"
    
            xtw.Flush();            // clean up
            xtw.Close();
        }
    }
    }
    

    输出:

    <songs>
    <![CDATA[<div class='ThumbContainer'>Some text here</div>]]>
    <songsTitle><![CDATA[sometexthtml here]]></songsTitle>
    </songs>
    

    想法是将示例中使用的静态文本替换为您从问题中提到的源文档中读取的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 2016-09-24
      • 2015-05-14
      • 1970-01-01
      • 2011-03-09
      • 1970-01-01
      相关资源
      最近更新 更多