gpt4 book ai didi

c# - 带有嵌套标签的 Xml 反序列化不起作用

转载 作者:数据小太阳 更新时间:2023-10-29 02:09:04 26 4
gpt4 key购买 nike

我需要将一个 XML 文件反序列化为一个对象。以下是 XML 内容:

<?xml version="1.0" encoding="utf-8" ?>
<PdfFile>
<PageTitle DocumentName="Sequence Diagram" Version="Version 4" >Title</PageTitle>
<LogoPath>C:\logo.png</LogoPath>
<Modules>
<Module Id="1" MainTitle="Module1">
<SubModules>
<SubModule>
<Title>SubModule1</Title>
<Path>SubModule1 Path</Path>
<Description>SubModule1 Desc</Description>
</SubModule>
<SubModule>
<Title>SubModule2</Title>
<Path>SubModule2 Path</Path>
<Description>SubModule2 Desc</Description>
</SubModule>
</SubModules>
</Module>
<Module Id="2" MainTitle="Module2">
<SubModules>
<SubModule>
<Title>SubModule1</Title>
<Path>SubModule1 Path</Path>
<Description>SubModule1 Desc</Description>
</SubModule>
</SubModules>
</Module>
</Modules>
</PdfFile>

以下是我为上述 xml 文件创建的类文件。

    using System;
using System.Xml.Serialization;

namespace PDFCreation.Objects
{

[Serializable]
[XmlRoot("PdfFile")]
public class PdfFile2
{
[XmlElement("PageTitle")]
public PageTitle FirstPage { get; set; }

[XmlElement("LogoPath")]
public string LogoPath { get; set; }

[XmlArray("Modules")]
[XmlArrayItem("Module", typeof(Module))]
public Module[] Modules { get; set; }

}

[Serializable]
public class Module
{
[XmlAttributeAttribute("Id")]
public int Id { get; set; }

[XmlAttributeAttribute("MainTitle")]
public string MainTitle { get; set; }

[XmlArray("SubModules")]
[XmlArrayItem("SubModule", typeof(SubModule))]
public SubModule[] Modules { get; set; }
}

[Serializable]
public class SubModule
{
[XmlElement("Title")]
public string Title { get; set; }

[XmlElement("Path")]
public string Path { get; set; }

[XmlElement("Description")]
public string Description { get; set; }
}

[Serializable]
public class PageTitle
{
[XmlText]
public string Title { get; set; }

[XmlAttribute]
public string DocumentName { get; set; }

[XmlAttribute]
public string Version { get; set; }
}

}

在反序列化时,我没有收到任何错误。但是 PdfFile 对象中的模块总是返回 null。我尝试使用 xsd.exe 生成的类。但同样的事情仍在发生。

请帮我找出代码/xml 中的问题以及为什么它没有完全反序列化?

谢谢!!!

编辑:

我的 C# 代码:

  public class Program
{
private static readonly string XmlPath = ConfigurationManager.AppSettings["XmlPath"];

static void Main(string[] args)
{
try
{
ReadXml();
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
Console.ReadLine();
}
}

private static void ReadXml()
{
if (!File.Exists(XmlPath))
{
Console.WriteLine("Error: Xml File Not exists in the path: {0}", XmlPath);
return;
}

using (var reader = new StreamReader(XmlPath))
{
var serializer = new XmlSerializer(typeof(PdfFile2));
var result = (PdfFile2)serializer.Deserialize(reader);

//other code here
}
}
}

最佳答案

只使用您提供的代码/xml 并放入缺少的结束标记,我使用此主类将其反序列化:

using System.IO;
using System.Xml.Serialization;
using PDFCreation.Objects;

public class Test
{
static void Main(string[] args)
{
XmlSerializer ser = new XmlSerializer(typeof(PdfFile2));
PdfFile2 pdf = (PdfFile2)ser.Deserialize(File.OpenRead("test.xml"));
}
}

你的反序列化代码是什么样的?

关于c# - 带有嵌套标签的 Xml 反序列化不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9991813/

26 4 0