码迷,mamicode.com
首页 > Windows程序 > 详细

C#解析XML为List集合

时间:2020-10-13 17:37:52      阅读:32      评论:0      收藏:0      [点我收藏+]

标签:elements   返回   void   document   element   nbsp   oid   cal   lis   

string strPayPenalty = @" ??xml version=""1.0"" encoding=""GBK""?>
< MAPS >< PRIVATE >< SUCFLAG > 0 </ SUCFLAG >< RESULT > 查询成功 </ RESULT ></ PRIVATE >< APPROOT >< PRIVATE >< List >< debtQuery >< currentDebt > 39290 </ currentDebt >< item1 > 39290 </ item1 >< item2 > *际祥 </ item2 >< item3 > 1 </ item3 >< item4 > 510132196802014214 </ item4 >< item5 > 5101320053 </ item5 >< item6 > 5101320053 </ item6 >< item7 > 19290 </ item7 >< item8 > 20000 </ item8 >< item9 ></ item9 >< item10 ></ item10 ></ debtQuery ></ List ></ PRIVATE ></ APPROOT ></ MAPS > ";
string[] strXML = strPayPenalty.Split(new string[] { "xml" }, StringSplitOptions.RemoveEmptyEntries);
strPayPenalty = "<?xml" + strXML[1];
strPayPenalty = strPayPenalty.Replace(" ??", "<?").Replace(" ", "").Replace("\n", "").Replace(@"<?xmlversion=""1.0""encoding=""GBK""?>", @"<?xml version=""1.0"" encoding=""GBK""?>").Trim();
PayPenaltyAnalytical1(strPayPenalty);

 

static List<PayPenaltyInquiryModel> itemsPRIVATE = null;
static List<PayPenaltyDetailsModel> debtQuery = null;
public static void PayPenaltyAnalytical1(string strPayPenalty)
{
string xmlString = strPayPenalty;
XDocument doc = XDocument.Parse(xmlString);
if (doc.Root != null)
{
itemsPRIVATE = (from r in doc.Root.Elements("PRIVATE")
select new PayPenaltyInquiryModel
{
SUCFLAG = (string)r.Element("SUCFLAG"),
RESULT = (string)r.Element("RESULT"),

}).ToList();
debtQuery = (from r in doc.Root.Elements("APPROOT").Elements("PRIVATE").Elements("List").Elements("debtQuery")
select new PayPenaltyDetailsModel
{
item1 = (string)r.Element("item1"),
item2 = (string)r.Element("item2"),
item3 = (string)r.Element("item3"),
item4 = (string)r.Element("item4"),
item5 = (string)r.Element("item5"),
item6 = (string)r.Element("item6"),
item7 = (string)r.Element("item7"),
item8 = (string)r.Element("item8"),
item9 = (string)r.Element("item9"),
item10 = (string)r.Element("item10"),
}).ToList();
//itemsPRIVATE = (from r in doc.Root.Elements("PRIVATE")
// select new PayPenaltyInquiryModel
// {
// SUCFLAG = (string)r.Element("SUCFLAG"),
// RESULT = (string)r.Element("RESULT"),
// debtQuery = debtQuery,
// }).ToList();

//foreach (var item in itemsPRIVATE)
//{
// item.debtQuery = debtQuery;
//}
itemsPRIVATE.First().debtQuery = debtQuery;
}
}

 

 

/// <summary>
/// 现场缴罚查询数据返回
/// </summary>
public class PayPenaltyInquiryModel
{
/// <summary>
/// 返回码
/// </summary>
public string SUCFLAG { get; set; }
/// <summary>
/// 数据长度
/// </summary>
public string RESULT { get; set; }

public List<PayPenaltyDetailsModel> debtQuery { get; set; }
}
/// <summary>
/// 违章数据查询明细
/// </summary>
public class PayPenaltyDetailsModel
{
/// <summary>
///
/// </summary>
public string item1 { get; set; }
/// <summary>
///
/// </summary>
public string item2 { get; set; }
/// <summary>
///
/// </summary>
public string item3 { get; set; }
/// <summary>
///
/// </summary>
public string item4 { get; set; }
/// <summary>
///
/// </summary>
public string item5 { get; set; }
/// <summary>
///
/// </summary>
public string item6 { get; set; }
/// <summary>
///
/// </summary>
public string item7 { get; set; }
/// <summary>
///
/// </summary>
public string item8 { get; set; }
/// <summary>
///
/// </summary>
public string item9 { get; set; }
/// <summary>
///
/// </summary>
public string item10 { get; set; }
}

C#解析XML为List集合

标签:elements   返回   void   document   element   nbsp   oid   cal   lis   

原文地址:https://www.cnblogs.com/zhaiganggang/p/13807161.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!