使用xmlreader只读取特定实例

时间:2012-10-17 14:27:27

标签: c# xml xmlreader

我有一个看起来像

的xml文件
<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>

我正在使用C#3.0中的xmlreader读取元素的所有值。但现在我必须通过只读取特定开始和结束时的值来改变。对于上面提到的xml文件,我需要默认读取<Name><Age>然后我有一个返回值“I1”或“I2”的函数,它基本上是元素名称。如果它返回“I1”,那么我应该只读取<I1></I1>之间的元素,不应该读取<I2>,反之亦然。所以代码结构(只是逻辑请忽略语法错误)如

/******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>**/   

    }
  }
}

谢谢!

1 个答案:

答案 0 :(得分:0)

所以假设,因为我们没有任何其他标签,所以你的文件从头到尾看起来像这样的东西

<?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>

然后调用它

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;
    }
相关问题