在C#中使用XmlReader?

时间:2019-07-02 18:00:39

标签: c# xml-parsing

我需要解析一个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>

我想将所有内容存储在字符串中作为字符串。组中所有内容的一个字符串,与属性ID无关。我将如何使用XMLReader做到这一点?

2 个答案:

答案 0 :(得分:1)

您可以选择使用序列化或XMLReader中的数据集来加载类: 课:

[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; }
}

和示例:

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);

因此,您应该在选项1 DataSet中具有多个表,在选项2中:已加载cobject“ root”: result image

答案 1 :(得分:0)

使用XML Reader,您可以申请

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);

 }
}

您可以创建一个类并使用属性进行映射

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);
     }
}