【问题标题】:XML returns NULL on XmlSerializer Deserialize DotNet Core C# (Console App)XML 在 XmlSerializer Deserialize DotNet Core C#(控制台应用程序)上返回 NULL
【发布时间】:2020-04-28 19:14:16
【问题描述】:

我知道这个问题并不新鲜,但经过一段时间的搜索,我决定在这里寻求帮助。 我的 XML 反序列化始终为空,我希望您对我的代码有想法。

private static bool MyJson(string mxl)
{
   XmlRootAttribute xRoot = new XmlRootAttribute();
                xRoot.ElementName = "Client";
                xRoot.IsNullable = true;
                var serializer = new XmlSerializer(typeof(MyDTO), xRoot);
                MyDTO result;
                using (TextReader reader = new StreamReader(xml))
                {
                   result = serializer.Deserialize(reader) as MyDTO;
                }
...
}

我的 XML

<Client>
    <References>
        <Reference>TE01234</Reference>
    </References>
    <Intro>
        <Name>Test</Name>
    </Intro>
    <Details>
        <Claimant>
            <Title>Mr</Title>
            <FirstName>Cxxx</FirstName>
            <Surname>Msyy</Surname>
        </Claimant>
        <Claimant2>
            <Title2>Mrs</Title2>
            <FirstName2>Xmsxx</FirstName2>
            <Surname2>Cktol</Surname2>
        </Claimant2>
    </Details>
</Client>

我的模特

public class MyDTO
{
   public string Reference {get;set;}
   public string Name {get;set;}
   public string Title {get;set;}
   public string FirstName {get;set;}
   public string Surname {get;set;}
   public string Title2 {get;set;}
   public string FirstName2 {get;set;}
   public string Surname2 {get;set;}
}

'结果;始终为空。

感谢您的帮助。

【问题讨论】:

  • 您的 DTO 模型似乎与您的 XML 架构不匹配。例如,您有一些包含一个(或更多)单数标签的“列表”(复数标签)。你可以看看:stackoverflow.com/questions/14783406/… 例如,或者尝试寻找“带有嵌套标签”的简单示例,有很多。
  • 您展示的示例 C# 代码也无法编译,您的某些属性缺少某些类型
  • 您的 XML 也无效。标签&lt;Claimant&gt; 似乎被&lt;/primaryClaimant&gt;“关闭”了

标签: c# xml .net-core


【解决方案1】:

使用 xml Linq :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;


namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            List<MyDTO> dtos = doc.Descendants("Client").Select(x => new MyDTO()
            {
                Reference = (string)x.Descendants("Reference").FirstOrDefault(),
                Name = (string)x.Descendants("Name").FirstOrDefault(),
                Title = (string)x.Descendants("Title").FirstOrDefault(),
                FirstName = (string)x.Descendants("FirstName").FirstOrDefault(),
                Surname = (string)x.Descendants("Surname").FirstOrDefault(),
                Title2 = (string)x.Descendants("Title2").FirstOrDefault(),
                FirstName2 = (string)x.Descendants("FirstName2").FirstOrDefault(),
                Surname2 = (string)x.Descendants("Surname2").FirstOrDefault()
            }).ToList();

        }
    }
    public class MyDTO
    {
       public string Reference {get;set;}
       public string Name {get;set;}
       public string Title {get;set;}
       public string FirstName {get;set;}
       public string Surname {get;set;}
       public string Title2 {get;set;}
       public string FirstName2 {get;set;}
       public string Surname2 {get;set;}
    }


}

【讨论】:

    猜你喜欢
    • 2018-02-16
    • 2017-09-10
    • 1970-01-01
    • 2017-08-17
    • 1970-01-01
    • 2016-12-07
    • 1970-01-01
    • 1970-01-01
    • 2018-02-09
    相关资源
    最近更新 更多