public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
#region Base64加密解密
/// <summary>
/// Base64是一種使用64基的位置計數法。它使用2的最大次方來代表僅可列印的ASCII 字元。
/// 這使它可用來作為電子郵件的傳輸編碼。在Base64中的變數使用字元A-Z、a-z和0-9 ,
/// 這樣共有62個字元,用來作為開始的64個數字,最後兩個用來作為數字的符號在不同的
/// 系統中而不同。
/// Base64加密
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string Base64Encrypt(string str)
{
byte[] encbuff = System.Text.Encoding.UTF8.GetBytes(str);
return Convert.ToBase64String(encbuff);
}
/// <summary>
/// Base64解密
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string Base64Decrypt(string str)
{
if (string.IsNullOrWhiteSpace(str))
{
return "";
}
byte[] decbuff = Convert.FromBase64String(str);
return System.Text.Encoding.UTF8.GetString(decbuff);
}
public static string StrToBase64(string str, bool encrypt = true)
{
try
{
if (encrypt)
{
byte[] b = System.Text.Encoding.GetEncoding("GBK").GetBytes(str);
//转成 Base64 形式的 System.String
return Convert.ToBase64String(b);
}
else
{
return str;
}
}
catch (Exception)
{
return "";
}
}
///解码
public static string DecodeBase64(string code)
{
string decode = "";
byte[] bytes = Convert.FromBase64String(code);
try
{
decode = System.Text.Encoding.GetEncoding("GBK").GetString(bytes);
}
catch
{
decode = code;
}
return decode;
}
private static string formatXml(object xml)
{
XmlDocument xd;
if (xml is XmlDocument)
{
xd = xml as XmlDocument;
}
else
{
xd = new XmlDocument();
xd.LoadXml(xml as string);
}
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
XmlTextWriter xtw = null;
try
{
xtw = new XmlTextWriter(sw);
xtw.Formatting = Formatting.Indented;
xtw.Indentation = 1;
xtw.IndentChar = '\t';
xd.WriteTo(xtw);
}
finally
{
if (xtw != null)
xtw.Close();
}
return sb.ToString();
}
#endregion
private static void TraverseXmlNode(XmlNode node)
{
if (node == null)
{
return;
}
if (node.HasChildNodes)
{
foreach (XmlNode childNode in node.ChildNodes)
{
TraverseXmlNode(childNode);
}
}
else
{
if (string.IsNullOrWhiteSpace(node.Value))
{
return;
}
node.Value = DecodeBase64(node.Value);
}
}
private static void TraverseXmlNodeStr(XmlNode node)
{
if (node == null)
{
return;
}
if (node.HasChildNodes)
{
foreach (XmlNode childNode in node.ChildNodes)
{
TraverseXmlNodeStr(childNode);
}
}
else
{
string pattern = @"(<!--.*?--\>)";///注释部分去除
if (string.IsNullOrWhiteSpace(node.Value) || Regex.IsMatch(node.OuterXml, pattern))
{
return;
}
node.Value = StrToBase64(node.Value);
}
}
private void button_Yes_Click(object sender, RoutedEventArgs e)
{
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(OldXml.Text);
TraverseXmlNode(xmlDoc.DocumentElement);
//2. 获取XML文件中的所有节点
NewXml.Text = formatXml(xmlDoc.InnerXml);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void button_No_Click(object sender, RoutedEventArgs e)
{
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(OldXml.Text);
TraverseXmlNodeStr(xmlDoc.DocumentElement);
//2. 获取XML文件中的所有节点
NewXml.Text = formatXml(xmlDoc.InnerXml);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}