zoukankan      html  css  js  c++  java
  • XML解析异常:根级别上的数据无效。 第 1 行 位置 1。

    异常信息:
     
    使用XDocument.Parse解析XML时报异常: 根级别上的数据无效。 第 1 行 位置 1。
     
     
    异常环境:
    1、客户端使用的是Framework2.0的XmlDocument生成Xml文件并传给服务端
    2、服务端使用的是Framework4.0的XDocument.Parse解析上传的Xml文件内容,解析内容时抛出上传异常
     
     
    分析原因:
    1、google、百度后基本确定是文件编码的问题,XmlDocument.Save()生成的文件有时可以解析有时又不行
    2、可以解析的xml文件编码属性为:(NotePad++查看)
    3、不能解析的xml文件编码属性为:
    4、只要让XmlDocument.Save()生成的文件格式符合可以解析标准即可
     
     
    解决方法:
    使用XmlDocument.Save()直接保存XML有编码问题,使用下述方法即可
     
    //设置xml生成样式
    XmlWriterSettings xmlSetting = new XmlWriterSettings();
    xmlSetting.Encoding = new UTF8Encoding(false);
    xmlSetting.Indent = true;

    //保存xml文件
    XmlWriter writer = XmlWriter.Create(filePath, xmlSetting);
    xmldoc.Save(writer);
    writer.Close();

    注:
    filePath:保存的文件路径
    xmldoc:XmlDocment对象





  • 相关阅读:
    owlsuddimatchmaker安装
    类集
    jsp基本语法
    心得思路记录下
    nyoj517 最小公倍数
    poj1250 Tanning Salon
    poj1686 Lazy Math Instructor
    poj3250 Bad Hair Day
    poj1047 Round and Round We Go
    poj2359 Questions
  • 原文地址:https://www.cnblogs.com/gossip/p/3030416.html
Copyright © 2011-2022 走看看