如何从 xml 读取嵌套对象?

发布于 2024-11-25 04:51:52 字数 1020 浏览 1 评论 0原文

我有一个看起来像这样的 xml 文件,

<questions>
  <question>
    <text>What color is an orange?</text>
    <answer>blue</answer>
    <answer>yellow</answer>
    <answer>orange</answer>
  </question>
  <question>
     <text>What color is a banana?</text> ...

我已经设法弄清楚如何使用对象的公共方法将属性和值读取到属性中,但是我如何获得包含“答案”的“问题”对象对象,序列化比使用 linq-to-xml 更好

这是使用 linq:

        var data = from query in questionData.Descendants("question")
                   select new Quiz.Question
                   {
                       QuestionTitle = (string)query.Attribute("title"),
                       QuestionText = query.Element("text") != null ? query.Element("text").Value.Trim() : string.Empty,
                       QuestionImage = query.Element("image") != null ? query.Element("image").Attribute("src").Value : string.Empty

...

在 linq 中,我如何将另一个节点序列化为另一个对象,假设我在“中有一个“答案”对象列表问题”?

I have an xml file that looks something like this

<questions>
  <question>
    <text>What color is an orange?</text>
    <answer>blue</answer>
    <answer>yellow</answer>
    <answer>orange</answer>
  </question>
  <question>
     <text>What color is a banana?</text> ...

I've managed to figure out how to read attributes and values into the properties using the public methods for the object, but how would i get a "Question" object that would contain "Answer" objects, would it be better to just serialize than use linq-to-xml

This is using linq:

        var data = from query in questionData.Descendants("question")
                   select new Quiz.Question
                   {
                       QuestionTitle = (string)query.Attribute("title"),
                       QuestionText = query.Element("text") != null ? query.Element("text").Value.Trim() : string.Empty,
                       QuestionImage = query.Element("image") != null ? query.Element("image").Attribute("src").Value : string.Empty

...

in linq how do I go about serializing another node as another object, say i have a list of "answer" object in "question"?

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

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

发布评论

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

评论(1

和我恋爱吧 2024-12-02 04:51:52

您可以为此使用序列化,但如果您想有一种完全可定制的方式来执行此操作,我会推荐这样做:

在问题类中:

public static Question FromXmlElement(XElement el)
{
    return new Question
    {
        Text = el.Element("Text").Value,
        Answers = el.Elements("Answer").Select(a=>a.Value);
    };
}

并且当您想阅读时:

var xdoc = XDocument.Parse(xml);
var questions = xdoc.Element("Questions").Elements("Question")
            .Select(e=> Question.FromXmlElement(e));

从 FromXmlElement 内部,您可以调用另一个复杂类型的相同方法,如果你的类有一个复杂类型的属性等等。

You can use serialization for this, but if you want to have a totally custimizable way of doing this I would recommend this:

In Question class:

public static Question FromXmlElement(XElement el)
{
    return new Question
    {
        Text = el.Element("Text").Value,
        Answers = el.Elements("Answer").Select(a=>a.Value);
    };
}

and when you want to read:

var xdoc = XDocument.Parse(xml);
var questions = xdoc.Element("Questions").Elements("Question")
            .Select(e=> Question.FromXmlElement(e));

from inside the FromXmlElement you can call same method of another complex type if your class has a property of a complex type and so on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文