如何将XML文件解析为包含字符串列表的类

如何将XML文件解析为包含字符串列表的类

本文介绍了如何将XML文件解析为包含字符串列表的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class ConvertedFilesMergeRule
{
   public List<string> MergingChannelIds { get; set; }
}







MergeRule = (from e in doc.Descendants("MergeRule")
                           select new ConvertedFilesMergeRule()
                           {
                                    MergingChannelIds= ??????????
                           }).ToList<ConvertedFilesMergeRule>







<MergeRule>
       <MergingChannelIds>
             <string>CTVCM</string>
             <string>TCM</string>
             <string>DTM</string>
       </MergingChannelIds>
</MergeRule>

推荐答案

MergeRule = (from e in doc.Descendants("MergeRule")
             let tmp=e
                           select new ConvertedFilesMergeRule()
{
                         MergingChannelIds= (from item e.Descendants("MergingChannelIds") select item.Value).ToList();
                           }).ToList<ConvertedFilesMergeRule>


using System.IO;
using System;
using System.Collections.Generic;
using System.Xml;

class MyClass
{
    int main()
    {
       List<string> strings = new List<string>();

       string file = @"C:\example.txt";
       StreamReader sr = new StreamReader(file);
       XmlTextReader tr = new XmlTextReader(sr);
       while(tr.Read())
       {
          if (tr.NodeType == XmlNodeType.Text)
              strings.Add(tr.Value)
       }
       tr.Close();
       sr.Close();

       //Do stuff...
    }
}


这篇关于如何将XML文件解析为包含字符串列表的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 01:09