XML文件的优缺点:
使用XML作为传输格式的优势:
1. 格式统一, 符合标准
2. 容易与其他系统进行远程交互, 数据共享比较方便
3.调用将 XML 用作传输的现有服务。
4.使用 XSLT 可以动态转换 XML。这是企业服务总线 (ESB) 方案中的理想功能。
缺点:
1. XML文件格式文件庞大, 格式复杂, 传输占用带宽;
2. 服务器端和客户端都需要花费大量代码来解析XML, 不论服务器端和客户端代码变的异常复杂和不容易维护;
3. 客户端不同浏览器之间解析XML的方式不一致, 需要重复编写很多代码;
4. 服务器端和客户端解析XML花费资源和时间;
现在大家大多数都用的json但是XML文件的重要性不可忽略,XML文件也是比较脆弱的!
拼接的字符串写入到XML文件中:
public static void WriteXMLWay()
{
StringBuilder sb = new StringBuilder();
sb.Append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
sb.Append("<sitemapindex><sitemap>");
for (int i = ; i <= GetSitmapCount(); i++)
{
sb.Append(GetSiteMapFormat(string.Format("http://www.website.com/sitemap/shenma/detail_{0}.xml", i)));
}
sb.Append("</sitemap></sitemapindex>");
try
{
//方法一
XmlDocument xd = new XmlDocument();
xd.LoadXml(sb.ToString());
xd.Save(@"\\sitemap.xml");
//方法二
File.WriteAllText(@"\\sitemap.xml", sb.ToString(), Encoding.UTF8);
}
catch (Exception ex)
{
XX.General.Exception.Error(ex, "ExecuteUpdateXmlforShenMa");
}
}
去除XML文件里面的特殊字符:
public static string FormatXmlText(string strHtml)
{
string[] aryReg = { "'", "<", ">", "%", "\"\"", ",", ".", ">", "<", "\\", ">=", "=<", "-", "_", ";", "||", "[", "]", "&", "/", "-", "|", " ", };
for (int i = ; i < aryReg.Length; i++)
{
strHtml = strHtml.Replace(aryReg[i], string.Empty);
}
return strHtml.Replace("[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]", "");
}
XML序列化时生成CDATA节点解决方法:
public partial class Person
{
[XmlIgnore]
public string Name { get; set; } [XmlIgnore]
public int Age { get; set; }
} public partial class Person
{
[XmlElement("Name")]
public XmlNode aaa
{
get
{
XmlNode node = new XmlDocument().CreateNode(XmlNodeType.CDATA, "", "");
node.InnerText = Name;
return node;
}
set { } //省略则aaa不会被序列化
}
[XmlElement("Age")]
public XmlNode bbb
{
get
{
XmlNode node = new XmlDocument().CreateNode(XmlNodeType.Text, "", "");
node.InnerText = Age.ToString();
return node;
}
set { } //省略则bbb不会被序列化
}
} class Program
{
static void Main(string[] args)
{
string result = string.Empty;
Person person = new Person() { Name = "dnawo", Age = };
using (MemoryStream output = new MemoryStream())
{
XmlSerializer serializer = new XmlSerializer(person.GetType());
serializer.Serialize(output, person);
result = Encoding.UTF8.GetString(output.ToArray());
}
Console.WriteLine(result); Console.ReadKey();
}
}
输出的XML代码:
<?xml version="1.0"?>
<Person xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Name><![CDATA[dnawo]]></Name>
<Age></Age>
</Person>
字符串写入XML编码的问题:
XmlDocument xd = new XmlDocument();
using (Utf8StringWriter sw = new Utf8StringWriter())
{
try
{
XmlSerializer xz = new XmlSerializer(shenma.GetType());
xz.Serialize(sw, shenma);
xd.LoadXml(sw.ToString());
xd.Save(string.Format(@"\\SiteMap\shenma\detail_{0}.xml", i));
}
catch (Exception ex)
{
XX.General.Exception.Error(ex, "ExecuteUpdateXmlforShenMa");
}
finally
{
redisclient.SetString("sitemap_shenma_novelid", novelid.ToString());
System.Threading.Thread.Sleep();
}
}
//让stringreader的编码默认为utf-8string
public sealed class Utf8StringWriter : StringWriter
{
public override Encoding Encoding { get { return Encoding.UTF8; } }
}
个人观点:XML文件逐渐被JSON代替,XML文件比较脆弱,用着没JSON好用!