C# 读写XML基本操作封装类
XML文件格式
<?xml version="1.0" encoding="gb2312"?>
<DataConfig>
<DisplayAttributes>
<!-- 1.生化 2.血球 3.B超 4.胸透 5.心电 6.LIS 7.尿检 8.骨密度 9.血流变 -->
<ShowItem Note="设备类型">53 69 249 148 117 202 165 40 247 94 190 235 167 27 191 78 </ShowItem>
<!-- 1.SQLServer 2.Access 3.MySQL 4.SQLite 5.Excel 6.Firebird 7.串口 8.端口-->
<DataType Note="传输类型">46 104 79 113 141 165 71 213 214 218 103 198 161 70 224 75 </DataType>
</DisplayAttributes>
<NameItem>
<!--图片存放位置-->
<IMG1 Note="图片" NormalHighValue="" NormalLowValue="" LisItem="" TableName="b_jktjb" ThirdParty="" Type="3">mediaPath</IMG1>
</NameItem>
<!-- 公共接口参数 -->
<PublicSocketConfig>
<TjbhFields Note="体检编号字段" >ClinicNumber</TjbhFields>
<Item Note="检查项目名称" >ChemName</Item>
<Result Note="结果" >RawResult</Result>
</PublicSocketConfig>
<!-- 界面参数 -->
<InterfaceConfig>
<DatabaseInstanceName Note="第三方数据库地址" >.</DatabaseInstanceName>
<DatabaseName Note="第三方数据库名" >DatabaseName</DatabaseName>
<UserName Note="第三方数据库用户名" >sa</UserName>
<Password Note="第三方数据库密码" >123456</Password>
</InterfaceConfig>
</DataConfig>
全局定义
/// <summary>
/// XML全局提取项目字段
/// </summary>
public static object initFields = new System.Dynamic.ExpandoObject();
初始化
/// <summary>
/// 将XML中的内容全部读取出来
/// </summary>
/// <param name="xElements"></param>
/// <param name="parentNodeName"></param>
/// <param name="elementDic"></param>
/// <param name="dictionaryValue"></param>
public static void initElementsItemName(IEnumerable<System.Xml.Linq.XElement> xElements, bool SystemParameters, string TabName, dynamic HealthItem, dynamic ThirdPartyItem, dynamic LisNameItem)
{
dynamic SystemParametersList = new System.Dynamic.ExpandoObject();
foreach (System.Xml.Linq.XElement item in xElements)
{
if (item.HasElements) { initElementsItemName(item.Elements(), SystemParameters, TabName, HealthItem, ThirdPartyItem, LisNameItem); }
else
{
try
{
if (!SystemParameters)
{
bool TypeYes = false;
dynamic lisDataList = new System.Dynamic.ExpandoObject();
if (!SystemParameters && item.Attributes().Count() != 0)
{
if (item.Attribute("Note") != null) { (lisDataList as System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, object>>).Add(new System.Collections.Generic.KeyValuePair<string, object>("Note", item.Attribute("Note").Value)); }
else { item.SetAttributeValue("Note", string.Empty); (lisDataList as System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, object>>).Add(new System.Collections.Generic.KeyValuePair<string, object>("Note", item.Attribute("Note").Value)); }
if (item.Attribute("NormalHighValue") != null) { (lisDataList as System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, object>>).Add(new System.Collections.Generic.KeyValuePair<string, object>("NormalHighValue", item.Attribute("NormalHighValue").Value)); }
else { item.SetAttributeValue("NormalHighValue", string.Empty); (lisDataList as System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, object>>).Add(new System.Collections.Generic.KeyValuePair<string, object>("NormalHighValue", item.Attribute("NormalHighValue").Value)); }
if (item.Attribute("NormalLowValue") != null) { (lisDataList as System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, object>>).Add(new System.Collections.Generic.KeyValuePair<string, object>("NormalLowValue", item.Attribute("NormalLowValue").Value)); }
else { item.SetAttributeValue("NormalLowValue", string.Empty); (lisDataList as System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, object>>).Add(new System.Collections.Generic.KeyValuePair<string, object>("NormalLowValue", item.Attribute("NormalLowValue").Value)); }
if (item.Attribute("LisItem") != null) { (lisDataList as System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, object>>).Add(new System.Collections.Generic.KeyValuePair<string, object>("LisItem", item.Attribute("LisItem").Value)); }
else { item.SetAttributeValue("LisItem", string.Empty); (lisDataList as System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, object>>).Add(new System.Collections.Generic.KeyValuePair<string, object>("LisItem", item.Attribute("LisItem").Value)); }
if (item.Attribute("TableName") != null) { (lisDataList as System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, object>>).Add(new System.Collections.Generic.KeyValuePair<string, object>("TableName", item.Attribute("TableName").Value)); }
else { item.SetAttributeValue("TableName", string.Empty); (lisDataList as System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, object>>).Add(new System.Collections.Generic.KeyValuePair<string, object>("TableName", item.Attribute("TableName").Value)); }
if (item.Attribute("ThirdParty") != null) { (lisDataList as System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, object>>).Add(new System.Collections.Generic.KeyValuePair<string, object>("ThirdParty", item.Attribute("ThirdParty").Value)); }
else { item.SetAttributeValue("ThirdParty", string.Empty); (lisDataList as System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, object>>).Add(new System.Collections.Generic.KeyValuePair<string, object>("ThirdParty", item.Attribute("ThirdParty").Value)); }
if (item.Attribute("Type") != null)
{
if (int.Parse(item.Attribute("Type").Value.ToString()) == Model.ParsingDataMethod.WindowsFormHackerHK.ShowItem) { TypeYes = true; }
}
else { item.SetAttributeValue("Type", string.Empty); }
}
if (TypeYes)
{
if (item.Value.ToString() != string.Empty)
{
(lisDataList as System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, object>>).Add(new System.Collections.Generic.KeyValuePair<string, object>("Health", item.Name.ToString()));
(ThirdPartyItem as System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, object>>).Add(new System.Collections.Generic.KeyValuePair<string, object>(item.Value.ToString(), lisDataList));
if (item.Attribute("LisItem").Value != string.Empty)
{ (LisNameItem as System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, object>>).Add(new System.Collections.Generic.KeyValuePair<string, object>(item.Attribute("LisItem").Value, lisDataList)); }
}
(lisDataList as System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, object>>).Add(new System.Collections.Generic.KeyValuePair<string, object>("Item", item.Value.ToString()));
(HealthItem as System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, object>>).Add(new System.Collections.Generic.KeyValuePair<string, object>(item.Name.ToString(), lisDataList));
}
}
else if (SystemParameters) { if ("DisplayAttributes" == item.Name.ToString()) { (SystemParametersList as System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, object>>).Add(new System.Collections.Generic.KeyValuePair<string, object>(item.Name.ToString(), item.Value.ToString())); } else { (SystemParametersList as System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, object>>).Add(new System.Collections.Generic.KeyValuePair<string, object>(item.Name.ToString(), item.Value.ToString())); } }
}
catch (Exception) { continue; }
}
}
if (TabName != string.Empty) { ((System.Collections.Generic.IDictionary<string, object>)initFields).Remove(TabName); (initFields as System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, object>>).Add(new System.Collections.Generic.KeyValuePair<string, object>(TabName, SystemParametersList)); ; }
return;
}
/// <summary>
/// 将XML中的内容全部读取出来
/// </summary>
/// <param name="xElements"></param>
/// <param name="parentNodeName"></param>
/// <param name="elementDic"></param>
/// <param name="dictionaryValue"></param>
public static void initElementItem(IEnumerable<System.Xml.Linq.XElement> xElements, string TableHeader)
{
dynamic HealthItem = new System.Dynamic.ExpandoObject();
dynamic ThirdPartyItem = new System.Dynamic.ExpandoObject();
dynamic LisNameItem = new System.Dynamic.ExpandoObject();
foreach (System.Xml.Linq.XElement item in xElements)
{
if (item.HasElements)
{
if (item.Name.ToString() != "DisplayAttributes" && item.Name.ToString() != "PublicSocketConfig" && item.Name.ToString() != "InterfaceConfig")
{
initElementsItemName(item.Elements(), false, "", HealthItem, ThirdPartyItem, LisNameItem);
}
else if (item.Name.ToString() == "DisplayAttributes" || item.Name.ToString() == "PublicSocketConfig" || item.Name.ToString() == "InterfaceConfig")
{
initElementsItemName(item.Elements(), true, item.Name.ToString(), HealthItem, ThirdPartyItem, LisNameItem);
}
}
}
((System.Collections.Generic.IDictionary<string, object>)initFields).Remove("HealthItem");
((System.Collections.Generic.IDictionary<string, object>)initFields).Remove("ThirdPartyItem");
((System.Collections.Generic.IDictionary<string, object>)initFields).Remove("LisNameItem");
(initFields as System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, object>>).Add(new System.Collections.Generic.KeyValuePair<string, object>("HealthItem", HealthItem));
(initFields as System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, object>>).Add(new System.Collections.Generic.KeyValuePair<string, object>("ThirdPartyItem", ThirdPartyItem));
(initFields as System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, object>>).Add(new System.Collections.Generic.KeyValuePair<string, object>("LisNameItem", LisNameItem));
}
/// <summary>
/// 初始化提取检查项目
/// </summary>
public static bool InitializingFields(string tableHeader = "")
{
bool Success = false;
try
{
System.Xml.Linq.XDocument xmlDocument = System.Xml.Linq.XDocument.Load(@".\Config\Config.xml");
Dictionary<string, string> elementDic = new Dictionary<string, string>();
initElementItem(xmlDocument.Root.Elements(), tableHeader);
if (((System.Collections.Generic.IDictionary<string, object>)initFields).ContainsKey(Enum.GetName(typeof(BLL.InitializationParameters.ItemType), BLL.InitializationParameters.ItemType.DisplayAttributes)))
{
xmlDocument.Save(@".\Config\Config.xml"); Success = true;
}
}
catch (Exception) { Success = false; }
return Success;
}
保存
/// <summary>
/// 将XML中的内容全部读取出来
/// </summary>
/// <param name="xElements"></param>
/// <param name="parentNodeName"></param>
/// <param name="elementDic"></param>
/// <param name="dictionaryValue"></param>
public static bool SaveFieldsElementsItemName(IEnumerable<System.Xml.Linq.XElement> xElements, string Health, string ItemName, string NormalHighValue, string NormalLowValue, string LisItem, string ThirdParty)
{
bool Success = false;
try
{
foreach (System.Xml.Linq.XElement item in xElements)
{
if (Success)
{
return Success;
}
else if (item.HasElements)
{
Success = SaveFieldsElementsItemName(item.Elements(), Health, ItemName, NormalHighValue, NormalLowValue, LisItem, ThirdParty);
}
else
{
if (item.Name.ToString() == Health)
{
if (item.Attributes().Count() != 0)
{
if (NormalHighValue != string.Empty)
{
item.SetAttributeValue("NormalHighValue", NormalHighValue);
}
else if (NormalLowValue != string.Empty)
{
item.SetAttributeValue("NormalLowValue", NormalLowValue);
}
else if (LisItem != string.Empty)
{
item.SetAttributeValue("LisItem", LisItem);
}
else if (ThirdParty != string.Empty)
{
item.SetAttributeValue("ThirdParty", ThirdParty);
}
}
if (ItemName != "")
{
item.Value = ItemName;
}
Success = true;
}
}
}
}
catch (Exception)
{
Success = false;
}
return Success;
}
/// <summary>
/// 将XML中的内容全部读取出来
/// </summary>
/// <param name="xElements"></param>
/// <param name="parentNodeName"></param>
/// <param name="elementDic"></param>
/// <param name="dictionaryValue"></param>
public static bool SaveFieldsElementItem(IEnumerable<System.Xml.Linq.XElement> xElements, string Health, string ItemName, string tableHeader, string NormalHighValue, string NormalLowValue, string LisItem, string ThirdParty)
{
bool Success = false;
foreach (System.Xml.Linq.XElement item in xElements)
{
if (item.HasElements)
{
if (Success)
{
return Success;
}
else
{
Success = SaveFieldsElementsItemName(item.Elements(), Health, ItemName, NormalHighValue, NormalLowValue, LisItem, ThirdParty);
}
}
}
return Success;
}
//保存
public static void SaveFieldsConfig(string Health, string ItemName = "", string tableHeader = "", string NormalHighValue = "", string NormalLowValue = "", string LisItem = "", string ThirdParty = "")
{
try
{
if (Health != string.Empty && Health != " ")
{
System.Xml.Linq.XDocument xmlDocument = System.Xml.Linq.XDocument.Load(@".\Config\Config.xml");
Dictionary<string, string> elementDic = new Dictionary<string, string>();
if (SaveFieldsElementItem(xmlDocument.Root.Elements(), Health, ItemName, tableHeader, NormalHighValue, NormalLowValue, LisItem, ThirdParty))
{
xmlDocument.Save(@".\Config\Config.xml");
InitializingFields();
}
}
}
catch (Exception)
{
Environment.Exit(1);
}
}
读
if (((System.Collections.Generic.IDictionary<string, object>)initFields).ContainsKey("InterfaceConfig"))
{
if (((System.Collections.Generic.IDictionary<string, object>)((System.Collections.Generic.IDictionary<string, object>)initFields)["InterfaceConfig"]).ContainsKey("DatabaseInstanceName"))
{
string str = ((System.Collections.Generic.IDictionary<string, object>)((System.Collections.Generic.IDictionary<string, object>)initFields)["InterfaceConfig"])[d.Name.Split(new string[] { "_" }, StringSplitOptions.RemoveEmptyEntries)["DatabaseInstanceName"].ToString();
}
}
写
SaveFieldsConfig("InterfaceConfig", "DatabaseInstanceName","1123", "", "", "", "", "");
移除
((System.Collections.Generic.IDictionary<string, object>)initFields).Remove("InterfaceConfig");
添加
dynamic interfaceDisplay = new System.Dynamic.ExpandoObject();
(initFields as System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, object>>).Add(new System.Collections.Generic.KeyValuePair<string, object>("InterfaceConfig", interfaceDisplay));
//网络素材仅限收藏 方便学习