操作XML的XmlDeclaration

在数据库中使用openxml操作时,当xml字符串中含有中文时,结果sql对xml解析报错,xml内容如下:

<?xml version="1.0"?>
<order>
  <country>中国</country>
  <province>北京</province>
  <city>海淀区</city>
  <address>北京北京别急</address>
  <postalCode>100086</postalCode>
  <telephone>13811153770(62192343)</telephone>
  <email />
</order>

在把声明部分修改为<?xml version="1.0" encoding="gb2312" ?>后正常解析。

但由于xml的数据是从一个传入的字符串中加载来的,这个数据可能带头也可能会出现不带头的情况,在参考相关的资料后,通过如下的代码实现自己的需求。

XmlDocument xdoc  =   new  XmlDocument();
xdoc.LoadXml(strXml);

if  (xdoc.FirstChild.NodeType  ==  XmlNodeType.XmlDeclaration)
ExpandedBlockStart.gifContractedBlock.gif
{
    XmlDeclaration xmldecl 
= xdoc.FirstChild as XmlDeclaration;
    xmldecl.Encoding 
= "gb2312";
}

else
ExpandedBlockStart.gifContractedBlock.gif
{
    XmlDeclaration xmldecl 
= xdoc.CreateXmlDeclaration("1.0""gb2312"null);
    xdoc.InsertBefore(xmldecl, xdoc.DocumentElement);
}

转载于:https://www.cnblogs.com/mincyw/archive/2008/10/15/1312041.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值