C# NetCore XML 反序列化解析错误:<xml xmlns=‘‘> was not expected 及 Encoding=utf-16问题

本文描述了一位开发者在处理XML文件时遇到的解析异常,问题出在XML根元素的命名空间和编码上。通过确保类名与XML根元素匹配,或使用XmlRootAttribute指定根元素,以及修正StringWriter的默认Encoding为UTF-8,成功解决了反序列化失败和编码问题。提供的代码示例展示了如何创建自定义的StringUTF8Writer类以确保XML文件以UTF-8编码进行序列化。

xml帮助类在最后

刚好有业务需要解析xml文件,于是找到帮助类代码开始尝试解析文件,总是出现异常:<xml xmlns='xxxxxxxxxxxxxxxx'> was not expected,开始寻找解决方案:

要使结果正确,必须满足两个条件中的任意一个:
1. TResult 类的类名称和 XML 根元素名一致。
2. 若类名和根元素名称不一致,在初始化 XmlSerializer 时传入第二个参数,new XmlRootAttribute("【根元素名称】"),以指定根元素名称
否则 Xmlserializer.Deserialize 抛出如下异常:

System.InvalidOperationException: There is an error in XML document (1, 2). ---> System.InvalidOperationException: <xml xmlns=''> was not expected.
Result StackTrace:
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderTResult.Read3_RootElementName()

at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(TextReader textReader)

  

 

其实我的文件根节点与实体是一致的,添加 XmlRoot依然报错,继续找问题原因,各种调试后发现可能是xmlns命名空间不在xml规范内,于是指定命名空间:

果然反序列化成功了 。

 还有一个就是xml写入指定目录,于是开始按规则创建xml文件,实体创建后,使用SerializeObject序列化结果如下图:

正常应该是utf8,怎么出现utf16呢?经测试后发现原来是使用的StringWriter的问题,因此,提醒诸位,如果使用非Stream类的输出,如StringBuilder/StringWriter,作为XmlWriter输出的话,请注意你的xml的Encoding 

查看StringWriter发现了Encoding

于是重写StringWriter的Encoding:

/// <summary>
/// hef 2023.04.20 08:37 新建StringUTF8Writer类改写Encoding
/// StringWriter默认encoding="utf-16"改写为Encoding.UTF8
/// </summary>
public class StringUTF8Writer : System.IO.StringWriter
{
    public override Encoding Encoding
    {
        get { return Encoding.UTF8; }
    }
}

 至此,序列化xml文件内容成功。

 

全部代码如下:

    /// <summary>
    /// hef 2023.04.19 15:36 新建xml序列化帮助类
    /// </summary>
    public class XmlSerializeHelper
    {
        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="TValue">要序列化的对象</param>
        /// <returns></returns>
        static public string SerializeObject(object TValue)
        {
            if (TValue is null)
            {
                return null;
            }

            try
            {
                XmlSerializer xmlSerializer = new XmlSerializer(TValue.GetType());

                //using (StringWriter stringWriter = new StringWriter())
                using (StringUTF8Writer stringWriter = new StringUTF8Writer())
                {
                    using (XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter)
                    {
                        Formatting = Formatting.Indented,
                        Namespaces = false
                    })

                    {
                        xmlSerializer.Serialize(stringWriter, TValue);
                        return stringWriter.ToString();
                    }
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }

        /// <summary>
        /// 反序列化
        /// </summary>
        /// <typeparam name="T">要序列化的对象</typeparam>
        /// <param name="value">文件内容</param>
        /// <returns></returns>
        static public T DeserializeObject<T>(string value) where T : class, new()
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                return null;
            }

            try
            {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
                using (TextReader textReader = new StringReader(value))
                {
                    T t = new T();
                    t = (T)xmlSerializer.Deserialize(textReader);
                    return t;
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }
    }

    /// <summary>
    /// hef 2023.04.20 08:37 新建StringUTF8Writer类改写Encoding
    /// StringWriter默认encoding="utf-16"改写为Encoding.UTF8
    /// </summary>
    public class StringUTF8Writer : System.IO.StringWriter
    {
        public override Encoding Encoding
        {
            get { return Encoding.UTF8; }
        }
    }

我的solr 链接配置文件 为xml,内容如下。我有很多个.netcore,我想注入solr 链接来实现在Controller 中调用solr。xml内容:<?xml version="1.0" encoding="UTF-8"?> <castle> <facilities> <facility id='solr' type='Castle.Facilities.SolrNetIntegration.SolrNetFacility, Castle.Facilities.SolrNetIntegration'> <solrURL>http://127.0.0.1:8983/solr/</solrURL> <cores> <core id='MedTCMDisease'> <documentType>System.Collections.Generic.Dictionary`2[System.String,System.Object]</documentType> <url>http://127.0.0.1:8983/solr/MedTCMDisease</url> </core> <core id='MedTCM'> <documentType>System.Collections.Generic.Dictionary`2[System.String,System.Object]</documentType> <url>http://127.0.0.1:8983/solr/MedTCM</url> </core> <core id='MedTCMZhongChengYao'> <documentType>System.Collections.Generic.Dictionary`2[System.String,System.Object]</documentType> <url>http://127.0.0.1:8983/solr/MedTCMZhongChengYao</url> </core> <core id='MedTCMFangJi'> <documentType>System.Collections.Generic.Dictionary`2[System.String,System.Object]</documentType> <url>http://127.0.0.1:8983/solr/MedTCMFangJi</url> </core> <core id='MedTCMZhenJiu'> <documentType>System.Collections.Generic.Dictionary`2[System.String,System.Object]</documentType> <url>http://127.0.0.1:8983/solr/MedTCMZhenJiu</url> </core> <core id='MedTCM_L'> <documentType>System.Collections.Generic.Dictionary`2[System.String,System.Object]</documentType> <url>http://127.0.0.1:8983/solr/MedTCM_L</url> </core> <core id='MedTCMBianzhengLunzhi'> <documentType>System.Collections.Generic.Dictionary`2[System.String,System.Object]</documentType> <url>http://127.0.0.1:8983/solr/MedTCMBianzhengLunzhi</url> </core> <core id='MedTCMMulu'> <documentType>System.Collections.Generic.Dictionary`2[System.String,System.Object]</documentType> <url>http://127.0.0.1:8983/solr/MedTCMMulu</url> </core> </cores> </facility> </facilities> </castle>
最新发布
07-05
在 ASP.NET Core 8.0 的 MVC 项目中使用 SolrNet,并通过 XML 配置文件注入 Solr 客户端连接信息,可以通过依赖注入机制实现。SolrNet 支持通过配置文件定义 Solr 服务地址,并在运行时动态加载这些配置[^1]。 ### 创建 XML 配置文件 首先,在项目根目录下创建一个 XML 文件(例如 `solr.config.xml`),用于存储 Solr 的连接信息: ```xml <?xml version="1.0" encoding="utf-8"?> <solr> <server> <url>http://localhost:8983/solr/your_core_name</url> </server> </solr> ``` ### 注册 SolrNet 并读取 XML 配置 在 `Program.cs` 中注册 SolrNet 服务,并从 XML 配置文件中加载 Solr 地址: ```csharp using Microsoft.Extensions.DependencyInjection; using SolrNet; using System.IO; using System.Xml.Linq; var builder = WebApplication.CreateBuilder(args); // 读取 XML 配置文件中的 Solr 地址 var solrUrl = XDocument.Load("solr.config.xml").Element("solr")?.Element("server")?.Element("url")?.Value; // 注册 SolrNet 服务 builder.Services.AddSolrNet<Product>(solrUrl); builder.Services.AddControllersWithViews(); var app = builder.Build(); if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Home/Error"); app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); app.Run(); ``` ### 定义实体类并映射字段 定义一个实体类 `Product` 来表示 Solr 文档结构,并使用 `SolrField` 和 `SolrUniqueKey` 特性进行字段映射: ```csharp public class Product { [SolrUniqueKey("id")] public string Id { get; set; } [SolrField("name")] public string Name { get; set; } [SolrField("description")] public string Description { get; set; } [SolrField("price")] public decimal Price { get; set; } } ``` ### 在控制器中使用 SolrNet 客户端 在控制器中通过构造函数注入 `ISolrOperations<Product>` 接口,并执行查询、添加等操作: ```csharp using Microsoft.AspNetCore.Mvc; using SolrNet; using System.Threading.Tasks; public class ProductController : Controller { private readonly ISolrOperations<Product> _solr; public ProductController(ISolrOperations<Product> solr) { _solr = solr; } public async Task<IActionResult> Index() { // 查询所有文档 var results = await _solr.QueryAsync(SolrQuery.All); return View(results); } public async Task<IActionResult> Search(string query) { // 执行关键字搜索 var results = await _solr.QueryAsync(new SolrQuery(query)); return View("Index", results); } public async Task<IActionResult> Add() { var product = new Product { Id = "1", Name = "Sample Product", Description = "This is a sample product.", Price = 19.99m }; await _solr.AddAsync(product); await _solr.CommitAsync(); return RedirectToAction("Index"); } } ``` ### 使用视图展示 Solr 数据 在视图中绑定 `IEnumerable<Product>` 类型的数据,并展示 Solr 查询结果: ```html @model IEnumerable<SolrNet.Documentation.Product> <h2>Products</h2> <table class="table"> <thead> <tr> <th>ID</th> <th>Name</th> <th>Description</th> <th>Price</th> </tr> </thead> <tbody> @foreach (var item in Model) { <tr> <td>@item.Id</td> <td>@item.Name</td> <td>@item.Description</td> <td>@item.Price</td> </tr> } </tbody> </table> ``` ---
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hefeng_aspnet

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值