反序列化 XML

发布于 2025-01-04 19:02:45 字数 445 浏览 2 评论 0原文

我想在 C# (.net 2.0) 中反序列化 XML 文件。

XML 的结构如下:

<elements>
   <element>
     <id>
       123
     </id>
     <Files>
       <File id="887" description="Hello World!" type="PDF">
         FilenameHelloWorld.pdf
       </File>
     </Files>
   </element>
<elements>

当我尝试在 C# 中反序列化此结构时,我遇到了文件名问题,该值始终为 NULL,即使我尝试编写 File 类也是如此。

请帮我。 ;-)

I want to deserialize a XML file in C# (.net 2.0).

The structure of the XML is like this:

<elements>
   <element>
     <id>
       123
     </id>
     <Files>
       <File id="887" description="Hello World!" type="PDF">
         FilenameHelloWorld.pdf
       </File>
     </Files>
   </element>
<elements>

When I try to deserialize this structure in C#, I get a problem with the Filename, the value is always NULL, even how I try to code my File class.

Please help me. ;-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白云不回头 2025-01-11 19:02:45

以下对我来说效果很好:

public class element
{
    [XmlElement("id")]
    public int Id { get; set; }

    public File[] Files { get; set; }
}

public class File
{
    [XmlAttribute("id")]
    public int Id { get; set; }

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

    [XmlAttribute("type")]
    public string Type { get; set; }

    [XmlText]
    public string FileName { get; set; }
}

class Program
{
    static void Main()
    {
        using (var reader = XmlReader.Create("test.xml"))
        {
            var serializer = new XmlSerializer(typeof(element[]), new XmlRootAttribute("elements"));
            var elements = (element[])serializer.Deserialize(reader);

            foreach (var element in elements)
            {
                Console.WriteLine("element.id = {0}", element.Id);
                foreach (var file in element.Files)
                {
                    Console.WriteLine(
                        "id = {0}, description = {1}, type = {2}, filename = {3}", 
                        file.Id,
                        file.Description,
                        file.Type,
                        file.FileName
                    );
                }
            }

        }
    }
}

The following works fine for me:

public class element
{
    [XmlElement("id")]
    public int Id { get; set; }

    public File[] Files { get; set; }
}

public class File
{
    [XmlAttribute("id")]
    public int Id { get; set; }

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

    [XmlAttribute("type")]
    public string Type { get; set; }

    [XmlText]
    public string FileName { get; set; }
}

class Program
{
    static void Main()
    {
        using (var reader = XmlReader.Create("test.xml"))
        {
            var serializer = new XmlSerializer(typeof(element[]), new XmlRootAttribute("elements"));
            var elements = (element[])serializer.Deserialize(reader);

            foreach (var element in elements)
            {
                Console.WriteLine("element.id = {0}", element.Id);
                foreach (var file in element.Files)
                {
                    Console.WriteLine(
                        "id = {0}, description = {1}, type = {2}, filename = {3}", 
                        file.Id,
                        file.Description,
                        file.Type,
                        file.FileName
                    );
                }
            }

        }
    }
}
踏雪无痕 2025-01-11 19:02:45

这应该有效...

[XmlRoot("elements")]
public class Elements {
    [XmlElement("element")]
    public List<Element> Items {get;set;}
} 
public class Element { 
    [XmlElement("id")]
    public int Id {get;set;}
    [XmlArray("Files")]
    [XmlArrayItem("File")]
    public List<File> Files {get;set;}
}
public class File {
    [XmlAttribute("id")]
    public int Id {get;set;}
    [XmlAttribute("description")]
    public string Description {get;set;}
    [XmlAttribute("type")]
    public string Type {get;set;}
    [XmlText]
    public string Filename {get;set;}
}

特别注意使用不同的属性来表示不同的含义。已验证(修复 xml 的结束元素后):

string xml = @"..."; // your xml, but fixed

Elements root;
using(var sr = new StringReader(xml))
using(var xr = XmlReader.Create(sr)) {
    root = (Elements) new XmlSerializer(typeof (Elements)).Deserialize(xr);
}
string filename = root.Items[0].Files[0].Filename; // the PDF

This should work...

[XmlRoot("elements")]
public class Elements {
    [XmlElement("element")]
    public List<Element> Items {get;set;}
} 
public class Element { 
    [XmlElement("id")]
    public int Id {get;set;}
    [XmlArray("Files")]
    [XmlArrayItem("File")]
    public List<File> Files {get;set;}
}
public class File {
    [XmlAttribute("id")]
    public int Id {get;set;}
    [XmlAttribute("description")]
    public string Description {get;set;}
    [XmlAttribute("type")]
    public string Type {get;set;}
    [XmlText]
    public string Filename {get;set;}
}

Note in particular the use of different attributes for different meanings. Verified (after fixing the closing element of your xml):

string xml = @"..."; // your xml, but fixed

Elements root;
using(var sr = new StringReader(xml))
using(var xr = XmlReader.Create(sr)) {
    root = (Elements) new XmlSerializer(typeof (Elements)).Deserialize(xr);
}
string filename = root.Items[0].Files[0].Filename; // the PDF
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文