如何将XML解析为C#对象

问题描述 投票:-1回答:1

我有以下xml。如何将此XML解析为C#对象?我将尝试使用XMLserializer和模型上的Attributes来执行此操作,但是它不起作用。

    <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
  <NewDataSet xmlns="">
    <dtEmp diffgr:id="dtEmp1" msdata:rowOrder="0" diffgr:hasChanges="inserted">
      <ID>4</ID>
      <Name>Hypersoft</Name>
      <FName />
      <Alias />
      <RevierVon>0</RevierVon>
      <RevierBis>0</RevierBis>
      <ZugangAlleTische>1</ZugangAlleTische>
      <AutoKeyNr>0</AutoKeyNr>
      <CardUID>00000000</CardUID>
      <OrigKey>0F9A820100000000</OrigKey>
      <EmpFoto>weiss.jpg</EmpFoto>
      <FotoLink></FotoLink>
      <FotoData>sagsd</FotoData>
    </dtEmp>
    <dtEmp diffgr:id="dtEmp2" msdata:rowOrder="1" diffgr:hasChanges="inserted">
      <ID>5</ID>
      <Name>Kellner 01</Name>
      <FName />
      <Alias />
      <RevierVon>0</RevierVon>
      <RevierBis>0</RevierBis>
      <ZugangAlleTische>0</ZugangAlleTische>
      <AutoKeyNr>0</AutoKeyNr>
      <CardUID />
      <OrigKey />
      <EmpFoto>hoffmann.jpg</EmpFoto>
      <FotoLink></FotoLink>
      <FotoData>dsafasa</FotoData>
    </dtEmp>
  </NewDataSet>
</diffgr:diffgram>

我正在使用fowwong代码

 public T Deserialize(List<XElement> elements)
{
    try
    {

        object res = null;
        foreach (var element in elements)
        {
            if (!string.IsNullOrEmpty(element.Value))
            {
                XElement xElement = XElement.Parse(element.ToString());

                using (StringReader reader = new StringReader(xElement.ToString()))
                {
                    XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
                    var entity = (T) xmlSerializer.Deserialize(reader);
                    return entity;
                }
            }
        }

        return (T) res;
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
        throw;
    }
}

以下结果模型:

 [XmlRoot("diffgr:diffgram")]
    public class EmployeeResponse
    {
        [XmlArray("NewDataSet")]
        [XmlArrayItem("dtEmp")]
        public EmployeeDto[] Employees { get; set; }
    }

    public class EmployeeDto
    {
        [XmlAttribute("ID")]
        public int Id { get; set; }

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

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

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

        [XmlAttribute("RevierVon")]
        public int RevierVon { get; set; }

        [XmlAttribute("RevierBis")]
        public int RevierBis { get; set; }

        [XmlAttribute("ZugangAllTische")]
        public byte ZugangAllTische { get; set; }

        [XmlAttribute("AutoKeyNr")]
        public short AutoKeyNr { get; set; }

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

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

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

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

        [XmlAttribute("FotoData")]
        public byte[] FotoData { get; set; }
    }

我在foreach圈子中有某种反序列化的例外。我想我的类和字段的属性错误。

c# .net xml xml-parsing
1个回答
0
投票

尝试以下内容:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
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<XElement> diffgram = doc.Descendants().Where(x => x.Name.LocalName == "diffgram").ToList();
            MyDeserialize<EmployeeResponse> serializer = new MyDeserialize<EmployeeResponse>();
            EmployeeResponse response = (EmployeeResponse)serializer.Deserialize(diffgram);

        }
    }
    public class MyDeserialize<T> where T : new()
    {
        public T Deserialize(List<XElement> elements)
        {
            try
            {

                object res = null;
                foreach (var element in elements)
                {
                    if (!string.IsNullOrEmpty(element.Value))
                    {
                        XElement xElement = XElement.Parse(element.ToString());

                        using (StringReader reader = new StringReader(xElement.ToString()))
                        {
                            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
                            var entity = (T)xmlSerializer.Deserialize(reader);
                            return entity;
                        }
                    }
                }

                return (T)res;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
    }

    [XmlRoot(ElementName = "diffgram", Namespace = "urn:schemas-microsoft-com:xml-diffgram-v1")]
    public class EmployeeResponse
    {
        [XmlArray("NewDataSet", Namespace = "")]
        [XmlArrayItem("dtEmp", Namespace = "")]
        public EmployeeDto[] Employees { get; set; }


    }

    public class EmployeeDto
    {
        [XmlElement("ID")]
        public int Id { get; set; }

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

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

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

        [XmlElement("RevierVon")]
        public int RevierVon { get; set; }

        [XmlElement("RevierBis")]
        public int RevierBis { get; set; }

        [XmlElement("ZugangAllTische")]
        public byte ZugangAllTische { get; set; }

        [XmlElement("AutoKeyNr")]
        public short AutoKeyNr { get; set; }

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

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

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

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

        [XmlElement("FotoData")]
        public byte[] FotoData { get; set; }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.