XML编码格式转换问题,急!!!

babybei000 2010-03-09 11:12:00
我要在网页上上传一个XML文件,我在C#中写了一个验证XML文件格式的方法:
private bool ValidateXml(string xmlFilePath)
{
string xsdPath = Request.PhysicalApplicationPath + @"Files\example\rcpt.xsd";
//使用xmlReader验证XML格式
// Set the validation settings.
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(null, xsdPath);
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
settings.ValidationEventHandler += new ValidationEventHandler(settings_ValidationEventHandler);

// Create the XmlReader object.
XmlReader reader = XmlReader.Create(xmlFilePath, settings);

// Parse the file.
try
{
while (reader.Read());
}
catch (Exception ex)
{

lblMessage.Text = "出现异常!" + ex.Message;
return false;
}
reader.Close();
return isValidateXML;
}
现在出现的问题就是当上传的xml文件的编码格式是UTF-8而且文件里面有中文的时候,在走这个验证的时候就会格式不通过,我需要把utf-8转化成gb2312,才能通过这个验证。但是我不知道要怎么转换,请高手指教。
上传的xml文件入下:
<?xml version="1.0" encoding="UTF-8" ?>
<orders>
<order rcptNo="2820204">
<invcId>6.68003E+11</invcId>
<dlverCd>804</dlverCd>
<stat>Z</stat>
<dlverName>山东</dlverName>
<desc>desc</desc>
</order>
<order rcptNo="">
<invcId>668003057092</invcId>
<dlverCd>804</dlverCd>
<stat>Y</stat>
<dlverName>海红</dlverName>
<desc>desc2</desc>
</order>
</orders>
...全文
1026 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
zsyViolet 2010-11-11
  • 打赏
  • 举报
回复
用GBK的编码试试
ILOVE_ASPNET 2010-03-09
  • 打赏
  • 举报
回复
引用 4 楼 babybei000 的回复:
2楼的能不能说的清楚一点啊?


虽然是VB 语法但是和C# 都大致一样,可以试一下。。
spiderwolf2010 2010-03-09
  • 打赏
  • 举报
回复

Dim sr As StreamReader = New StreamReader("xml文件路径", System.Text.Encoding.GetEncoding("gb2312"))

Dim reader As Xml.XmlReader = Xml.XmlReader.Create(sr, settings)


不知道行不行,你自己试一下。
babybei000 2010-03-09
  • 打赏
  • 举报
回复
2楼的能不能说的清楚一点啊?
jimh 2010-03-09
  • 打赏
  • 举报
回复
看看schema文件的编码格式是不是utf-8
spiderwolf2010 2010-03-09
  • 打赏
  • 举报
回复
试试看 StreamReader 设定gb2312编码格式读入xml文件。
babybei000 2010-03-09
  • 打赏
  • 举报
回复
顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶,up up up up up up up up up up up up up up up up up up up up up up up up
spiderwolf2010 2010-03-09
  • 打赏
  • 举报
回复
你提供的xml文件无法在ie打开的。
babybei000 2010-03-09
  • 打赏
  • 举报
回复
引用 8 楼 babybei000 的回复:
up up up up up up up up up up up up up up up up up up up up up up up up up up up upup up up up up ~~~

babybei000 2010-03-09
  • 打赏
  • 举报
回复
up up up up up up up up up up up up up up up up up up up up up up up up up up up upup up up up up ~~~
babybei000 2010-03-09
  • 打赏
  • 举报
回复
2楼你的方法不行啊,还是验证中文不通过。

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧