如何通过读取空属性节点跳过 XML 阅读器?

编程入门 行业动态 更新时间:2024-10-27 20:28:09
本文介绍了如何通过读取空属性节点跳过 XML 阅读器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我想在这种 XML 文档中跳过空 id 父节点并移动到非空 id 父节点.目前我的程序使用 XmlTextReader 来读取和处理这个 XML.但有时记录 id 可能为空,那时我想跳过此记录父节点,阅读器应移至下一个节点而不读取该空 id 父节点.伙计们,你有什么想法吗?请帮帮我!!!

I want to skip empty id parent node and move to not empty id parent node in this kind of XML document.Currently my program using XmlTextReader to read and process this XML. But sometime record id can be empty and that time I want to skip this record parent node and reader should move to next node without reading that empty id parent node. Guys do you have any idea to do that ? Please help me !!!

`<record id="">
  <record><data></data></record>
  <record><data></data></record>
 </record>
 <record id="###">
  <record><data></data></record>
  <record><data></data></record>
 </record>

`

推荐答案

我喜欢结合使用 XmlReader 和 Xml Linq :

I like using a combination of XmlReader and 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)
        {
            XmlReader reader = XmlReader.Create(FILENAME);

            while (!reader.EOF)
            {
                if (reader.Name != "record")
                {
                    reader.ReadToFollowing("record");
                }
                if (!reader.EOF)
                {
                    XElement record = (XElement)XElement.ReadFrom(reader);
                    string id = (string)record.Attribute("id");
                    if (id.Length > 0)
                    {
                        Console.WriteLine("id = '{0}'", id.ToString());
                    }
                }
            }
            Console.ReadLine();
        }
    }
}

这篇关于如何通过读取空属性节点跳过 XML 阅读器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-24 02:48:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1054517.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:节点   阅读器   跳过   属性   XML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!