C method for converting Dictionary to xml files

  • 2020-12-19 21:09:24
  • OfStack

This article shows an example of how C# converts Dictionary to xml files. Share to everybody for everybody reference. Specific implementation methods are as follows:

Here is the xml file:

<?xml version="1.0" encoding="utf-8" ?>
<nodes>
< Land and resources bureau >
<name> Municipal Bureau of Land and Resources </name>
<code>330</code>
< To accept the telephone="88205156"> Ping, qian </ To accept the >
< audit personId="48e1bca3-b0f5d0fec89"> friends </ audit >
< Review and approval >123</ Review and approval >
<BELONGSYSTEM>37001</BELONGSYSTEM>
<DEPTID>10080100030</DEPTID>
<SERVICECODE>4e58a6f1</SERVICECODE>
</ Land and resources bureau >
< Land and resources bureau >
<name> County Land and Resources Bureau </name>
<code>3321</code>
< To accept the telephone="13819655058"> In the morning </ To accept the >
< audit personId="f7351d0f-b197-0a0fc685f3ac"> fai </ audit >
< Review and approval >456</ Review and approval >
<BELONGSYSTEM>123</BELONGSYSTEM>
<DEPTID>00100033</DEPTID>
<SERVICECODE>
204cdd0b
</SERVICECODE>
</ Land and resources bureau >
</nodes>

Here are the related acquisition methods:

/// <summary>
/// Get acceptance information
/// </summary>
/// <param name="p_shixianCode"> Cities and counties in coding </param>
/// <returns> To accept the information </returns>
public static  Dictionary<string,string> ShouLiInfo(string p_shixianCode)
{
   XDocument xd = null;
   string xmlPath = "config.xml";
   xd = XDocument.Load(xmlPath);//xml Store path    Dictionary<string, string> pDic = new Dictionary<string, string>();
   var info = from t in xd.Root.Descendants(" Land and resources bureau ").Where(p => p.Element("code").Value == p_shixianCode) select new { name = t.Element("name").Value, code = t.Element("code").Value, shouli = t.Element(" To accept the ").Value, telephone = t.Element(" To accept the ").Attribute("telephone").Value, shenhe = t.Element(" audit ").Value, personId = t.Element(" audit ").Attribute("personId").Value, shending = t.Element(" Review and approval ").Value, DEPTID = t.Element("DEPTID").Value, BELONGSYSTEM = t.Element("BELONGSYSTEM").Value, SERVICECODE = t.Element("SERVICECODE").Value };
   foreach (var item in info)
   {
       pDic.Add("name", item.name);
       pDic.Add("code", item.code);
       pDic.Add("shouliPerson", item.shouli);
       pDic.Add("telephone", item.telephone);
       pDic.Add("shenhePerson", item.shenhe);
       pDic.Add("shenhepersonId", item.personId);
       pDic.Add("shendingPerson", item.shending);
       pDic.Add("DEPTID", item.DEPTID);
       pDic.Add("BELONGSYSTEM", item.BELONGSYSTEM);
       pDic.Add("SERVICECODE", item.SERVICECODE);
   }
   return pDic;
}

And that's it. Just call that method

Hopefully this article has helped you with your C# programming.


Related articles: