<tfoot id='HK8nM'></tfoot>

      • <bdo id='HK8nM'></bdo><ul id='HK8nM'></ul>
    1. <small id='HK8nM'></small><noframes id='HK8nM'>

    2. <i id='HK8nM'><tr id='HK8nM'><dt id='HK8nM'><q id='HK8nM'><span id='HK8nM'><b id='HK8nM'><form id='HK8nM'><ins id='HK8nM'></ins><ul id='HK8nM'></ul><sub id='HK8nM'></sub></form><legend id='HK8nM'></legend><bdo id='HK8nM'><pre id='HK8nM'><center id='HK8nM'></center></pre></bdo></b><th id='HK8nM'></th></span></q></dt></tr></i><div id='HK8nM'><tfoot id='HK8nM'></tfoot><dl id='HK8nM'><fieldset id='HK8nM'></fieldset></dl></div>
      <legend id='HK8nM'><style id='HK8nM'><dir id='HK8nM'><q id='HK8nM'></q></dir></style></legend>

      删除 XML 节点

      时间:2023-08-26
      <tfoot id='2Wk1S'></tfoot>

    3. <i id='2Wk1S'><tr id='2Wk1S'><dt id='2Wk1S'><q id='2Wk1S'><span id='2Wk1S'><b id='2Wk1S'><form id='2Wk1S'><ins id='2Wk1S'></ins><ul id='2Wk1S'></ul><sub id='2Wk1S'></sub></form><legend id='2Wk1S'></legend><bdo id='2Wk1S'><pre id='2Wk1S'><center id='2Wk1S'></center></pre></bdo></b><th id='2Wk1S'></th></span></q></dt></tr></i><div id='2Wk1S'><tfoot id='2Wk1S'></tfoot><dl id='2Wk1S'><fieldset id='2Wk1S'></fieldset></dl></div>

      <small id='2Wk1S'></small><noframes id='2Wk1S'>

        <legend id='2Wk1S'><style id='2Wk1S'><dir id='2Wk1S'><q id='2Wk1S'></q></dir></style></legend>

          <tbody id='2Wk1S'></tbody>

              <bdo id='2Wk1S'></bdo><ul id='2Wk1S'></ul>
              1. 本文介绍了删除 XML 节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我还有另一项无法完成的任务:我应该从 此站点,删除所有名称中没有VIDEO"的节点,然后将其保存到另一个 XML 文件中.我在阅读和写作方面没有问题,但是删除让我有些困难.我试图做节点 -> 父节点 -> 子节点工作,但它似乎没有用:

                I have got yet another task I am not able to accomplish: I am supposed to parse the XML from this site, remove all the nodes that don't have "VIDEO" in their name and then save it to another XML file. I have no problems with reading and writing, but removing makes me some difficulties. I have tried to do the Node -> Parent Node -> Child Node work-aroud, but it did not seem useful:

                static void Main(string[] args)
                    {
                        using (WebClient wc = new WebClient())
                        {
                            string s = wc.DownloadString("http://feeds.bbci.co.uk/news/health/rss.xml");
                            XmlElement tbr = null;
                            XmlDocument xml = new XmlDocument();
                            xml.LoadXml(s);
                
                            foreach (XmlNode node in xml["rss"]["channel"].ChildNodes)
                            {
                                if (node.Name.Equals("item") && node["title"].InnerText.StartsWith("VIDEO"))
                                {
                                    Console.WriteLine(node["title"].InnerText);
                                }
                                else
                                {
                                    node.ParentNode.RemoveChild(node);
                                }
                            }
                
                            xml.Save("NewXmlDoc.xml");
                            Console.WriteLine("
                Done...");
                
                            Console.Read();
                        }
                    }
                

                我也尝试了 RemoveAll 方法,但效果不佳,因为它删除了所有不满足VIDEO"条件的节点.

                I have also tried the RemoveAll method, which does not work as well, because it removes all the nodes not satisfying the "VIDEO" condition.

                //same code as above, just the else statement is changed
                else
                {
                   node.RemoveAll();
                }
                

                你能帮帮我吗?

                推荐答案

                我发现 Linq To Xml 更容易使用

                I find Linq To Xml easier to use

                var xDoc = XDocument.Load("http://feeds.bbci.co.uk/news/health/rss.xml");
                
                xDoc.Descendants("item")
                    .Where(item => !item.Element("title").Value.StartsWith("VIDEO"))
                    .ToList()
                    .ForEach(item=>item.Remove());
                
                xDoc.Save("NewXmlDoc.xml");
                

                你也可以使用XPath

                foreach (var item in xDoc.XPathSelectElements("//item[not(starts-with(title,'VIDEO:'))]")
                                         .ToList())
                {
                    item.Remove();             
                }
                

                这篇关于删除 XML 节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:使用 EasyNetQ for RabbitMQ 时自定义错误队列名称? 下一篇:使用命名空间 C# 解析 XML

                相关文章

                最新文章

              2. <legend id='9GZnH'><style id='9GZnH'><dir id='9GZnH'><q id='9GZnH'></q></dir></style></legend>
                <i id='9GZnH'><tr id='9GZnH'><dt id='9GZnH'><q id='9GZnH'><span id='9GZnH'><b id='9GZnH'><form id='9GZnH'><ins id='9GZnH'></ins><ul id='9GZnH'></ul><sub id='9GZnH'></sub></form><legend id='9GZnH'></legend><bdo id='9GZnH'><pre id='9GZnH'><center id='9GZnH'></center></pre></bdo></b><th id='9GZnH'></th></span></q></dt></tr></i><div id='9GZnH'><tfoot id='9GZnH'></tfoot><dl id='9GZnH'><fieldset id='9GZnH'></fieldset></dl></div>

                  <small id='9GZnH'></small><noframes id='9GZnH'>

                  <tfoot id='9GZnH'></tfoot>

                      <bdo id='9GZnH'></bdo><ul id='9GZnH'></ul>