How to write CData in xml

前端 未结 6 1543
谎友^
谎友^ 2020-12-06 00:09

i have an xml like :



    
             


        
6条回答
  •  误落风尘
    2020-12-06 00:30

    Use Node.InnerXml, not Node.InnerText. Node.InnerText is automatically replacing special values. Note that if you specify with CDATA in InnerXml, then Node.InnerText is text in CDATA. Example:

    public class Test
    {
        public static int Main(string[] args)
        {
            const string xmlTxt = @"
    
        
        
      ";
            TextReader treader = new StringReader(xmlTxt);
            XmlReader xreader = XmlReader.Create(treader);
            XmlDocument xdoc = new XmlDocument();
            xdoc.Load(xreader);
    
            XmlNode xnode = xdoc.SelectSingleNode("entry/entry_status");
            //xnode.InnerText = "";
            xnode.InnerXml = "";
            Console.WriteLine("inner text is: " + xnode.InnerText);
    
            xdoc.Save(Console.Out); Console.WriteLine();
    
            return 0;
        }
    }
    

    Program's output:

    inner text is: something
    
    
      
      
      
    
    

提交回复
热议问题