xml到数据结构

发布于 2024-09-17 06:37:24 字数 470 浏览 6 评论 0原文

我有一个如下所示的 xml 文件

<step>
    <id>3</id>
    <backid>1</backid>
    <question>Are you having a good day</question>
    <yesid>4</yesid>
    <noid>5</noid>
       </step>

它被设置为一个应用程序,询问用户是或否问题,以帮助指导用户完成复杂的业务流程。 yesid 和 noid 是该过程下一步的 id 号。在过去(以及在其他语言中),我会将 xml 文件中的信息加载到多维数组中,然后从那里开始。

不过,我正在尝试在此应用程序中使用 linq to xml,并且我想知道是否有比我过去所做的 xpath to array 更有效的方法

I have an xml file that looks like this

<step>
    <id>3</id>
    <backid>1</backid>
    <question>Are you having a good day</question>
    <yesid>4</yesid>
    <noid>5</noid>
       </step>

It's set up to be an application that asks the user yes and no questions to assist in guiding the user through a complicated business process. The yesid and noid are the id numbers of the next step of the process. In the past (and in other languages) I would have loaded the info from the xml file into a multidimensional array and gone from there.

However I am attempting to use linq to xml in this application and I am wondering if there is a more efficient way of doing this than the xpath to array that I have been doing in the past

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

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

发布评论

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

评论(1

护你周全 2024-09-24 06:37:24

我将创建一个表示步骤的类,使用 LINQ-to-XML 将 XML 文件读入其中,然后创建一个步骤字典以便于查找:(

var doc = XDocument.Load("xmlfile1.xml");

var steps = from step in doc.Root.Elements("step")
            select new Step
                   {
                       Id       = (int)step.Element("id"),
                       BackId   = (int)step.Element("backid"),
                       Question = (string)step.Element("question"),
                       YesId    = (int)step.Element("yesid"),
                       NoId     = (int)step.Element("noid"),
                   };

var dict = steps.ToDictionary(step => step.Id);

var currentStep = dict[3];

while (true)
{
    switch (Ask(currentStep.Question))
    {
        case Yes:  currentStep = dict[currentStep.YesId];  break;
        case No:   currentStep = dict[currentStep.NoId];   break;
        case Back: currentStep = dict[currentStep.BackId]; break;
        default:   return;
    }
}

假设该文件在公共根下包含多个元素。 )

I would create a class that represents a step, read the XML file into it using LINQ-to-XML, and then create a dictionary of steps for easy lookup:

var doc = XDocument.Load("xmlfile1.xml");

var steps = from step in doc.Root.Elements("step")
            select new Step
                   {
                       Id       = (int)step.Element("id"),
                       BackId   = (int)step.Element("backid"),
                       Question = (string)step.Element("question"),
                       YesId    = (int)step.Element("yesid"),
                       NoId     = (int)step.Element("noid"),
                   };

var dict = steps.ToDictionary(step => step.Id);

var currentStep = dict[3];

while (true)
{
    switch (Ask(currentStep.Question))
    {
        case Yes:  currentStep = dict[currentStep.YesId];  break;
        case No:   currentStep = dict[currentStep.NoId];   break;
        case Back: currentStep = dict[currentStep.BackId]; break;
        default:   return;
    }
}

(Assuming the file contains several <step> elements under a common root.)

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