简体   繁体   中英

using XmlReader in c#?

I need to parse an XML file.

The structure of the file is below:

<root> 
      <group id = "one">
             <info1>
                   <detail1> detail </detail1>
                   <detail2> detail </detail2>
             </info1>
             <info2>
                   <detail1> detail </detail1>
                   <detail2> detail </detail2>
             </info2>
      </group> 
      <group id = "two">
             <info1>
                   <detail1> detail </detail1>
                   <detail2> detail </detail2>
             </info1>
             <info2>
                   <detail1> detail </detail1>
                   <detail2> detail </detail2>
             </info2>
      </group> 

</root>

I would like to store everything within the group element as a string. One string for everything in group regardless of the attribute id. How would I do that with XMLReader?

you have options to load class by using serialization, or dataset from XMLReader: class:

[Serializable]
[XmlRoot(ElementName="root")]
public class root
{
    [System.Xml.Serialization.XmlElementAttribute("group")]
    public List<Group> group { get; set; }
}

[Serializable()]
public class Group
{
    [System.Xml.Serialization.XmlAttributeAttribute("id")]
    public string id { get; set; }

    public Info info1 { get; set; }
    public Info info2 { get; set; }
}

[Serializable()]
public class Info
{
    [XmlElement]
    public string detail1 { get; set; }
    [XmlElement]
    public string detail2 { get; set; }
}

and examlpes:

string xml = "<root><group id=\"one\"><info1><detail1>detail</detail1><detail2>detail</detail2></info1><info2><detail1>detail</detail1><detail2>detail</detail2></info2></group><group id=\"two\"><info1><detail1>detail</detail1><detail2>detail</detail2></info1><info2><detail1>detail</detail1><detail2>detail</detail2></info2></group></root>";
    XDocument x = XDocument.Parse(xml, LoadOptions.PreserveWhitespace);

    //option1
    XmlReader reader = x.CreateReader();
    DataSet ds = new DataSet();
    //DataSet will contain multiple tables
    ds.ReadXml(reader);

    //option 2
    XmlSerializer ser = new XmlSerializer(typeof(root));
    XmlReader reader2 = x.CreateReader();
    var res = ser.Deserialize(reader2);

as result you should have in option 1 DataSet with multiple tables, in option 2: loaded cobject "root": 结果图片

With XML Reader you can apply

List <string> stringList = new List <string>();

while (xmlReader.Read()) {
 if ((xmlReader.NodeType == XmlNodeType.Element) && (xmlReader.Name == "group")) {

  string str = string.Empty;
  foreach(var element in xmlReader) {
   str += element.value;

  }
  stringList.add(str);

 }
}

Or You can create a class and do the mapping with properties

class Group {

 public string info1 {
  get;
  set;
 }

 public string info2 {
  get;
  set;
 }

 public string details1 {
  get;
  set;
 }

 public string details2 {
  get;
  set;
 }
}


List<Group> groupList = new List <Group>();

while (xmlReader.Read()) {
    if ((xmlReader.NodeType == XmlNodeType.Element) && (xmlReader.Name == "group")) {

     Group group = new Group();

     foreach(XmlNode node in xmlReader) {
      switch (node.Name) {
       case "info1":
        group.info1 = node.Value;
        break;
       case "info2":
        group.info2 = node.Value;
        break;
       case "details1":
        group.details1 = node.Value;
        break;
       case "details2":
        group.details2 = node.Value;
        break;        
      }
      groupList.add(group);
     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM