开发者

Selecting certain sections of a XML file

Hi i have a C# windows form that has two list boxes on it. The left one contains a list of 16 student module codes that are available to enroll onto. The user selects a module from the left to transfer to the right box. I have it working up to this point, the module code transfers when the select button is clicked.

Once a module has been transfered I need to click on it and print out onto a label (placed to the right of it) the full module details that are held in an XML file.

I am able to read through the contents of the XML file and output that to the label (all contents). What I need it to do is just print out the "SELECTED module" details to the label. So I have to somehow read through the XML file and pick out the particular module/modules that the user selects? So if he/she selects 8 modules then I just want the details of all of those to display on the开发者_C百科 label?


Look up the documentation for System.Xml.XmlDocument or System.Xml.Linq.XDocument on MSDN. If your course is an XML elementm your code might look like this:

XmlDocument doc = new XmlDocument();            
        doc.Load(stream); // you can load it from stream, textreader or use LoadXml to init it from string
        XmlNodeList courseNodes = doc.DocumentElement.SelectNodes("/rootelement/courseelement"); // provide a valid Xpath here
        //work with courseNodes


var selectedNodes = doc.Descendants("node")
    .Where(x => (string)x.Attribute("type") == "selected1" || (string)x.Attribute("type") == "selected2" || etc...)
    .ToList();


You could use Linq to Xml. Here is a small sample

        private const string modulesXml =
        "<modules><module id =\"m1\">data1</module><module id =\"m2\">data2</module><module id =\"m3\">data3</module></modules>";

    public string GetModule(string id)
    {
        var modules = XDocument.Parse(modulesXml);
        return modules.Descendants("module").First(n => n.Attribute("id").Value == id).Value;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜