0

I have a xml file that looks like

<Name>AAA</Name>
<Age>23</Age>
<I1>
  <Element1>A</Element1>
  <Element2>B</Element2>
  <Element3>C</Element3>
<\I1>
<I2>
  <Element1>AA</Element1>
  <Element2>BB</Element2>
  <Element3>CC</Element3>
</I2>

I am reading all the values of elements using xmlreader in C# 3.0. But now i have to change by reading only the values within particular start and end tage. For the xml file mentioned above, i need to read <Name>, <Age> by default and then i have a function that returns the value "I1" or "I2" which is basically the element names. If it returns "I1" then i should read only the elements between <I1> and </I1> and should not read <I2> and vice versa. So the code structure would be (just the logic please ignore the syntax errors) like

/******function that returns element name I1 or I2*********/
string elementName = func(a,b);

xmlreader reader = reader.create("test.xml");
while(reader.read())
{
 switch(reader.nodetype)
 {
  case xmlnodetype.element:
   string nodeName = reader.name
   break;
 case xmlnodetype.text
   switch(nodeName)
   {
     /*************Read default name and age values*******/ 
     case "Name":
       string personName = reader.value
       break;
     case "Age":
       string personAge = reader.value;
       break;
    /*******End of reading default values**************/

    /*** read only elements between the value returned by function name above
        If function returns I1 then i need to read only values between <I1> </I1> else read </I2>    and </I2>**/   

    }
  }
}

Thanks!

techBeginner
  • 3,792
  • 11
  • 43
  • 59
Raghul Raman
  • 169
  • 1
  • 5
  • 18
  • Can't you use a newer version of .NET so you can make use of the XDocument class? That would make the task a whole lot easier. – Chrono Oct 17 '12 at 14:47
  • I may not be able to use. The code uses to read all the elements using xmlreader. If it is going to be really difficult to use xmlreader(seems to be) i will using xmldocument. I am working on xmldocument and see if i could work a way out here. The challenge seems to be how to read only the particular section (.. based on return value and exit out of the read. – Raghul Raman Oct 17 '12 at 15:42
  • `GetElementsByTagName` may help you or you can iterate over element nodes.. – techBeginner Oct 17 '12 at 15:57
  • Can you update the example of the xml providing the root element and the tags Name, age and Ix? – Sorceri Oct 17 '12 at 15:59

1 Answers1

0

So assuming, since we dont have any other tags to go off, that your file would look something such as this from beginning to end

<?xml version="1.0" encoding="utf-8" ?>
<RootElement>
    <UserInformation>
        <Name>AAA</Name>
        <Age>23</Age>
        <I1>    
            <Element1>A</Element1>
            <Element2>B</Element2>
            <Element3>C</Element3>
        <\I1>  
        <I2>    
            <Element1>AA</Element1>
            <Element2>BB</Element2>
            <Element3>CC</Element3>  
        </I2> 
    </UserInformation>
</RootElement>

and then to call it

System.IO.StreamReader sr = new System.IO.StreamReader("test.xml");
String xmlText = sr.ReadToEnd();
sr.Close();

List<UserInfo> finalList = readXMLDoc(xmlText);
if(finalList != null)
{
    //do something
}


    private List<UserInfo> readXMLDoc(String fileText)
    {
        //create a list of Strings to hold our user information
        List<UserInfo> userList = new List<UserInfo>();
        try
        {
            //create a XmlDocument Object
            XmlDocument xDoc = new XmlDocument();
            //load the text of the file into the XmlDocument Object
            xDoc.LoadXml(fileText);
            //Create a XmlNode object to hold the root node of the XmlDocument
            XmlNode rootNode = null;
            //get the root element in the xml document
            for (int i = 0; i < xDoc.ChildNodes.Count; i++)
            {
                //check to see if we hit the root element
                if (xDoc.ChildNodes[i].Name == "RootElement")
                {
                    //assign the root node
                    rootNode = xDoc.ChildNodes[i];
                    break;
                }
            }

            //Loop through each of the child nodes of the root node
            for (int j = 0; j < rootNode.ChildNodes.Count; j++)
            {
                //check for the UserInformation tag
                if (rootNode.ChildNodes[j].Name == "UserInformation")
                {
                    //assign the item node
                    XmlNode userNode = rootNode.ChildNodes[j];
                    //create userInfo object to hold results
                    UserInfo userInfo = new UserInfo();
                    //loop through each if the user tag's elements
                    foreach (XmlNode subNode in userNode.ChildNodes)
                    {
                        //check for the name tag
                        if (subNode.Name == "Name")
                        {
                            userInfo._name = subNode.InnerText;
                        }
                        //check for the age tag
                        if (subNode.Name == "Age")
                        {
                            userInfo._age = subNode.InnerText;
                        }
                        String tagToLookFor = "CallTheMethodThatReturnsTheCorrectTag";
                        //check for the tag
                        if (subNode.Name == tagToLookFor)
                        {
                            foreach (XmlNode elementNode in subNode.ChildNodes)
                            {
                                //check for the element1 tag
                                if (elementNode.Name == "Element1")
                                {
                                    userInfo._element1 = elementNode.InnerText;
                                }
                                //check for the element2 tag
                                if (elementNode.Name == "Element2")
                                {
                                    userInfo._element2 = elementNode.InnerText;
                                }
                                //check for the element3 tag
                                if (elementNode.Name == "Element3")
                                {
                                    userInfo._element3 = elementNode.InnerText;
                                }
                            }

                        }

                    }
                    //add the userInfo to the list
                    userList.Add(userInfo);
                }
            }
        }
        catch (Exception e)
        {
            System.Windows.Forms.MessageBox.Show(e.Message);
            return null;
        }
        //return the list
        return userList;
    }

    //struct to hold information
    struct UserInfo
    {
        public String _name;
        public String _age;
        public String _element1;
        public String _element2;
        public String _element3;
    }
Sorceri
  • 7,870
  • 1
  • 29
  • 38
  • Thanks a lot. I will try using XmlDocument as yourself and others suggested in this thread. The exact xml structure varies a little bit like but i understood your logic and will work on it. – Raghul Raman Oct 17 '12 at 17:55