如何使用C#递归打印XML树属性和元素?

发布于 2025-02-14 00:34:00 字数 2161 浏览 2 评论 0原文

我有以下XML文件,需要将作者的属性和儿童元素存储在变量中,然后在控制台上将其打印出来。因此,基本上是:ID,OBS和责任以及名称和姓氏需要存储为变量并将其打印到控制台上。

    <AUTHORS>
        <AUTHOR id='_03131488' obs='0' responsible='1'>
            <LASTNAME>Richard</LASTNAME>
            <FIRSTNAME>Dickson</FIRSTNAME>
        </AUTHOR>
        <AUTHOR id='_03135122' obs='0'>
            <LASTNAME>Carlo</LASTNAME>
            <FIRSTNAME>Ancelotti</FIRSTNAME>
        </AUTHOR>
        <AUTHOR id='_0312C456' obs='0' responsible='1'>
            <LASTNAME>Patricia</LASTNAME>
            <FIRSTNAME>Howard</FIRSTNAME>
        </AUTHOR>
    </AUTHORS>

我为此编写了以下代码,但某种程度上它根本没有打印到控制台上。

    class Solution
    {
        public static string xmlFilePath = "authors.xml";
    
        public static void Main()
        {
            XDocument doc = XDocument.Load(xmlFilePath);
            List<Author> authors = LoadAuthors(doc.Descendants("AUTHOR")
                .Elements("AUTHORS"));
        }
    
        public static List<Author> LoadAuthors(IEnumerable<XElement> authors)
        {
            return authors.Select(x => new Author()
            {
                FirstName = x.Attribute("FIRSTNAME").Value,
                LastName = x.Attribute("LASTNAME").Value,
                Children = LoadAuthors(x.Elements("AUTHORS"))
            }).ToList();
        }
    
        public void PrintAutors()
        {
            foreach (Author author in authors)
            {
                Console.WriteLine(author.ToString());
            }
        }
    }

    public class Author
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public List<Author> Children { get; set; }
    
        public override string ToString()
        {
            return $"{FirstName}, {LastName}\n{Children
                .Select(c => c.ToString())
                .Aggregate((a, b) => a + "\n" + b)}";
        }
    }

有人可以帮助我将值正确存储到变量上并将其打印到控制台上吗?

I have the following xml file and need to store the author's attributes and the children's elements in variables and print them out on the console. So basically: id, obs, and responsible as well as FIRSTNAME and LASTNAME need to be stored as variables and printed to the console.

    <AUTHORS>
        <AUTHOR id='_03131488' obs='0' responsible='1'>
            <LASTNAME>Richard</LASTNAME>
            <FIRSTNAME>Dickson</FIRSTNAME>
        </AUTHOR>
        <AUTHOR id='_03135122' obs='0'>
            <LASTNAME>Carlo</LASTNAME>
            <FIRSTNAME>Ancelotti</FIRSTNAME>
        </AUTHOR>
        <AUTHOR id='_0312C456' obs='0' responsible='1'>
            <LASTNAME>Patricia</LASTNAME>
            <FIRSTNAME>Howard</FIRSTNAME>
        </AUTHOR>
    </AUTHORS>

I have the following code written for it but somehow it doesn't print to console at all.

    class Solution
    {
        public static string xmlFilePath = "authors.xml";
    
        public static void Main()
        {
            XDocument doc = XDocument.Load(xmlFilePath);
            List<Author> authors = LoadAuthors(doc.Descendants("AUTHOR")
                .Elements("AUTHORS"));
        }
    
        public static List<Author> LoadAuthors(IEnumerable<XElement> authors)
        {
            return authors.Select(x => new Author()
            {
                FirstName = x.Attribute("FIRSTNAME").Value,
                LastName = x.Attribute("LASTNAME").Value,
                Children = LoadAuthors(x.Elements("AUTHORS"))
            }).ToList();
        }
    
        public void PrintAutors()
        {
            foreach (Author author in authors)
            {
                Console.WriteLine(author.ToString());
            }
        }
    }

    public class Author
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public List<Author> Children { get; set; }
    
        public override string ToString()
        {
            return 
quot;{FirstName}, {LastName}\n{Children
                .Select(c => c.ToString())
                .Aggregate((a, b) => a + "\n" + b)}";
        }
    }

Can someone help me with properly storing the values onto the variables and printing them to the console?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三寸金莲 2025-02-21 00:34:00

“@charlieface我是C#的新手,所以我对如何在主要方法中调用该调用

感到困惑?

        public static void Main()
        {
            XDocument doc = XDocument.Load(xmlFilePath);
            List<Author> authors = LoadAuthors(doc.Descendants("AUTHOR")
                .Elements("AUTHORS"));
            // New Line in Main() as suggested by @Charlieface
            PrintAutors(authors); 
        }

        // so that the signature of PrintAutors() would change a little
        private static void PrintAutors(IList<Author> authors)
        {
            foreach (Author author in authors)
            {
                Console.WriteLine(author.ToString());
            }
        }

"@Charlieface I am new to C# so I am confused about how to invoke that call in the main method?"

It could look like these two modifications:

        public static void Main()
        {
            XDocument doc = XDocument.Load(xmlFilePath);
            List<Author> authors = LoadAuthors(doc.Descendants("AUTHOR")
                .Elements("AUTHORS"));
            // New Line in Main() as suggested by @Charlieface
            PrintAutors(authors); 
        }

        // so that the signature of PrintAutors() would change a little
        private static void PrintAutors(IList<Author> authors)
        {
            foreach (Author author in authors)
            {
                Console.WriteLine(author.ToString());
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文