如何访问某个元素并检索数据XML

时间:2016-04-19 20:49:08

标签: c# xml

我的XML看起来像这样。我可以包含n个模块和n个赋值。分配嵌套在它包含的模块中:

<?xml version="1.0"?>
<Course name="engineering">
<Level4>
    <Module Name="electric" CreditVal="22">
        <Assignment Name="wer" Score="22" Weight="50">
        </Assignment>
        <Assignment Name="asd" Score="50" Weight="50">
        </Assignment>
    </Module>
    <Module Name="materials" CreditVal="22">
        <Assignment Name="ghj" Score="22" Weight="75>
        </Assignment>
        <Assignment Name="kl" Score="80" Weight="15">
        </Assignment>
    </Module>
</Level4>
</Course>

我通过以下方式访问模块和作业:

XPathDocument xpd = new XPathDocument("XMLFile.xml");

//getting modules in level 4
foreach (XPathNavigator mod in xpd.CreateNavigator().Select("/Course/Level4/Module"))
{
    //Accessing module elemtns
    if (mod.HasAttributes)
    {
        Module modtoadd = new Module();
        modtoadd.Name = mod.GetAttribute("Name", "");
        Console.WriteLine("module name: " + modtoadd.Name);
        modtoadd.CreditValue = int.Parse(mod.GetAttribute("CreditVal", ""));
        Console.WriteLine("module cred: " + modtoadd.CreditValue);
        modtoadd.Assignments = new List<Assignment>();
        //Accessing assignment elements within the module element
        foreach (XPathNavigator asgn in xpd.CreateNavigator().Select("Course/Level4/Module/Assignment"))
        {
            Assignment asn = new Assignment();
            asn.Name = asgn.GetAttribute("Name", "");
            Console.WriteLine(asn.Name);
            asn.Weighting = int.Parse(asgn.GetAttribute("Weight", ""));
            Console.WriteLine(asn.Weighting);
            asn.UsersScore = int.Parse(asgn.GetAttribute("Score", ""));
            Console.WriteLine(asn.UsersScore);
            modtoadd.Assignments.Add(asn);
        };
        courseXML.Level_41.Add(modtoadd);
    }
};

应该发生什么:xmlreader读取嵌套在它所驻留的相关模块中的赋值。如果我要总计模块分数,那么'eletric'应该值72,'材料'应该值102.

实际发生的事情:经过测试后我意识到逻辑有问题。保存一个模块时可以正常工作,但如果有多个模块,那么所有分配都将添加到每个模块中。如果我总计模块得分,那么材料和电子都值得174。

我是XML新手并阅读/保存到文件。谢谢你的帮助:)

2 个答案:

答案 0 :(得分:1)

我总觉得Linq2Xml更容易使用。

var xDoc = XDocument.Load(filename);
var res  = xDoc.Descendants("Module")
            .Select(m => new
            {
                Name = (string)m.Attribute("Name"),
                CreditVal = (int)m.Attribute("CreditVal"),

                Assignments = m.Descendants("Assignment")
                                .Select(a => new
                                {
                                    Name = (string)a.Attribute("Name"),
                                    Score = (int)a.Attribute("Score"),
                                    Weight = (int)a.Attribute("Weight")
                                })
                                .ToList()
            })
            .ToList();

答案 1 :(得分:0)

问题在于,您不是阅读当前Assignment下的mod,而是阅读文档中的每个Assignment

//foreach (XPathNavigator asgn in xpd.CreateNavigator().Select("Course/Level4/Module/Assignment"))
foreach (XPathNavigator asgn in mod.CreateNavigator().Select("Assignment"))

但是,请考虑使用@Eser提供的解决方案。它必须比你的方法更简单。