0

Here is my XML code(module.xml)

<module code="ECSE502">
  <code>ECSE502</code>
  <name>Algorithms and Data structures</name>
  <semester>1</semester>
  <prerequisites>none</prerequisites>
  <lslot>0</lslot>
  <tslot>1</tslot>
  <description>all about algorythms and data structers</description>
</module>
<module code="EIGA501">
  <code>EIGA501</code>
  <name>3D Grapgics I</name>
  <semester>1</semester>
  <prerequisites>none</prerequisites>
  <lslot>2</lslot>
  <tslot>3</tslot>
  <description>xxxxxxxxxxxxxxxxxxxxxx</description>
</module>

According to the above xml code, I need to get the set by only giving ECSE502 as the input. After selecting the required node I need to get its child node's values also(name, semester,etc.). In the XML file there are 20 nodes. this is only the 1st 2 nodes.

I tried this so far

 XmlTextReader reader = new XmlTextReader("modules.xml");
            XmlDocument doc = new XmlDocument();
            XmlNode node = doc.ReadNode(reader);

        foreach (XmlNode chldNode in node.ChildNodes)
            Console.WriteLine(reader.Value);
0

3 Answers 3

2

You can use LINQ to XML, assume in here the code returns the list of anonymous objects, but you can explicitly define your own class if you want:

var xDoc = XDocument.Load("yourpathfile");
var result = xDoc.Descendants("module")
            .Where(x => (string) x.Element("code") == "ECSE502")
            .Select(x => new
                             {
                                 Name = (string)x.Element("name"),
                                 //.....
                             });
Sign up to request clarification or add additional context in comments.

1 Comment

@Ravindu: try this way also :)
1

try this:

wrap your xml in a unique root node i.e

<modules>
   <module code...
       ..
   </module>
   <module code...
</modules>

and then

string text="ECSE502";

XmlDocument xml = new XmlDocument();
xml.Load("physical path to module.xml"); 

XmlNodeList xnList = xml.SelectNodes("modules/module[@code='"+text+"']");
foreach (XmlNode xn in xnList)
{
    string code = xn.SelectSingleNode("code").innerText;
    string name=  xn.SelectSingleNode("name").innerText;
    //and similarly find other inner nodes
}

5 Comments

not worked. and what does it do in line, XmlNodeList xnList = xml.SelectNodes("/module[@code='"+text+"']");
try wrapping your modules in a root tag.i.e <Modules><module.. and see the update
it finds only those module node which have their code attribute as the text
yeh it's working. But all the data is in one line as, ECSE502Algorithms and Data structures1none01 how can I divide them according to there nodes
1

Try it after creating a root node in your xml with below code

XDocument xdoc = XDocument.Load(@"D:\data\rvyas\Projects\Client\module.xml");


string code = "ECSE502";
    var result = xdoc.Descendants("module")
                .Where(x => (string)x.Element("code") == code)
                .Select(x => new
                {
                    Name = (string)x.Element("name"),
                    Code = (string)x.Element("code"),
                    semester = (string)x.Element("semester"),
                    prerequisites = (string)x.Element("prerequisites"),
                    lslot = (string)x.Element("lslot"),
                    tslot = (string)x.Element("tslot")

                }).ToList();

You XML should be like:

<root>
<module code="ECSE502">
  <code>ECSE502</code>
  <name>Algorithms and Data structures</name>
  <semester>1</semester>
  <prerequisites>none</prerequisites>
  <lslot>0</lslot>
  <tslot>1</tslot>
  <description>all about algorythms and data structers</description>
</module>
<module code="EIGA501">
  <code>EIGA501</code>
  <name>3D Grapgics I</name>
  <semester>1</semester>
  <prerequisites>none</prerequisites>
  <lslot>2</lslot>
  <tslot>3</tslot>
  <description>xxxxxxxxxxxxxxxxxxxxxx</description>
</module>
</root>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.