如何使用C#递归打印XML树属性和元素?
我有以下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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“@charlieface我是C#的新手,所以我对如何在主要方法中调用该调用
感到困惑?
"@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: