C# 生成XML时的特殊字符出现异常处理
生成端处理
将要写入的值的前后写上如:"<![CDATA[" + string+ "]]>";
XmlNode xnformchild = doc.CreateNode(XmlNodeType.Element, dc.ColumnName.ToUpper(), "");
try
{
xnformchild.InnerXml = drform[dc.ColumnName].ToString();
xnform.AppendChild(xnformchild);
}
catch
{
xnformchild.InnerXml = "<!--[CDATA[" + drform[dc.ColumnName].ToString() + "]]-->";
xnform.AppendChild(xnformchild);
}
接收端处理将生成端写入的:"<![CDATA["和 "]]>"分别替换即可
string startCdata = "<!--[CDATA[";
string endCdata = "]]-->";
if (fieldValue.StartsWith(startCdata) && (fieldValue.EndsWith(endCdata)))
{
fieldValue = fieldValue.Replace(startCdata, "");
fieldValue = fieldValue.Replace(endCdata, "");
}
简单吧,这样就可以解决xml存取特殊字符出现异常的问题,当然你在生成端只在存
有特殊字符的地方加上就行了,其它地方的不用加
相关文档:
//create a new Document
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document d = db.newDocument();
//add root Node
Element noteuser = d.createElement("note-users");
d.appendChil ......
XPath 语法
2007-06-05 17:24
XPath 语法
作者:w3pop.com 翻译/整理:w3pop.com 发布:2007-04-29 浏览:674 :: ::
XPath Nodes(节点) XPath Axes
XPath uses path expressions to select nodes or node-sets in an XML document. The node is selected by following a path or steps.
XPath 通过路径表达式从XM ......
读:
//打开某文件(假设web.config在根目录中)
string filename=Server.MapPath("/") + @"WebApplication1\web.config";
XmlDocument xmldoc= new XmlDocument();
xmldoc.Load(filename);
//得到顶层节点列表
XmlNodeList topM=xmldoc.DocumentElement.ChildNodes;
foreach(XmlElement element in topM)
{
if(ele ......
直接封装成一个类的,用起来还挺方便的
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text ......