C# Xml 反序列化问题

发布于 2024-10-03 11:02:52 字数 1532 浏览 4 评论 0原文

在我的项目中,遗留代码生成具有以下结构的 xml:

<Output>
    <Template recordID=12>
        <Employer type="String">
            <Value>Google</Value>
            <Value>GigaSoft inc.</Value>
        </Employer>
        <Designation  type="String">
            <Value>Google</Value>
        </Designation>
        <Duration  type="String" />
    </Template>
</Output>

我想将此 xml 反序列化为具有以下属性的对象(我使用 C#):

public class EmployerInfo
{
    string[] _employerName;
    string[] _designation;
    string _duration;
}

我尝试使用成员周围的以下属性来反序列化上述 xml (注意:我有简化的代码。我知道我们不应该公开数据成员。此代码仅用于实验目的)

[XmlElement("Template")]
public class EmployerInfo
{
    [XmlElement("Employer")]
    public string[] _employerName;

    [XmlElement("Designation")]
    public string[] _designation;

    [XmlElement("Duration")]
    public string _duration;
}

为了反序列化,在主类中我写道:

XmlSerializer serial = new XmlSerializer(typeof(Output));
TextReader reader = new StreamReader(@"C:\sample_xml.xml");
EmployerInfo fooBar = (EmployerInfo)serial.Deserialize(reader);
reader.Close();

执行上述代码后,fooBar对象中的所有成员都设置为null (默认值)。我认为这是因为xml结构与类结构不匹配。

我尝试使用 xsd 命令自动生成类,但它为每个数据成员创建了单独的类。

我尝试提供诸如 XmlElement("Employer.Value") 、 XmlElement("Template.Employer.Value") 之类的元素名称,但这也不起作用。

谁能建议一些方法将此 xml 放入 EmployerInfo 类中?

提前致谢

In my project, legacy code generates xml which has following structure :

<Output>
    <Template recordID=12>
        <Employer type="String">
            <Value>Google</Value>
            <Value>GigaSoft inc.</Value>
        </Employer>
        <Designation  type="String">
            <Value>Google</Value>
        </Designation>
        <Duration  type="String" />
    </Template>
</Output>

I want to deserialize this xml into object which has following properties (I am using C#):

public class EmployerInfo
{
    string[] _employerName;
    string[] _designation;
    string _duration;
}

I tried to deserialize above xml using following attributes around members (NOTE : I have simplified code. I know we should not make data members public. This code is just for experimental purpose)

[XmlElement("Template")]
public class EmployerInfo
{
    [XmlElement("Employer")]
    public string[] _employerName;

    [XmlElement("Designation")]
    public string[] _designation;

    [XmlElement("Duration")]
    public string _duration;
}

To deserialize, in main class I wrote :

XmlSerializer serial = new XmlSerializer(typeof(Output));
TextReader reader = new StreamReader(@"C:\sample_xml.xml");
EmployerInfo fooBar = (EmployerInfo)serial.Deserialize(reader);
reader.Close();

After executing above code, all the members in fooBar object are set to null (default values). I think this is because xml structure does not match with class structure.

I tried to automatically generate class using xsd command but it created seperate classes for each of the data member .

I tried to give element names like XmlElement("Employer.Value") , XmlElement("Template.Employer.Value") but this also didnt work.

Can anyone please suggest some way to fit this xml into a EmployerInfo class ?

Thanks in advance

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

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

发布评论

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

评论(1

活泼老夫 2024-10-10 11:02:52

尝试:

using System.IO;
using System.Xml.Serialization;
[XmlType("Template")]
public class EmployerInfo
{
    [XmlArray("Employer"), XmlArrayItem("Value")]
    public string[] _employerName;

    [XmlArray("Designation"), XmlArrayItem("Value")]
    public string[] _designation;

    [XmlElement("Duration")]
    public string _duration;
}
public class Output
{
    public EmployerInfo Template { get; set; }
}
static class Program
{
    static void Main()
    {
        XmlSerializer serial = new XmlSerializer(typeof(Output));
        using (var reader = new StringReader(@"<Output>
    <Template recordID=""12"">
        <Employer type=""String"">
            <Value>Google</Value>
            <Value>GigaSoft inc.</Value>
        </Employer>
        <Designation  type=""String"">
            <Value>Google</Value>
        </Designation>
        <Duration  type=""String"" />
    </Template>
</Output>"))
        {
            EmployerInfo fooBar = ((Output)serial.Deserialize(reader)).Template;
        }
    }
}

另请注意,从 XmlSerializer(typeof(Output)) 的反序列化方法返回的类型将是 Output 记录。

Try:

using System.IO;
using System.Xml.Serialization;
[XmlType("Template")]
public class EmployerInfo
{
    [XmlArray("Employer"), XmlArrayItem("Value")]
    public string[] _employerName;

    [XmlArray("Designation"), XmlArrayItem("Value")]
    public string[] _designation;

    [XmlElement("Duration")]
    public string _duration;
}
public class Output
{
    public EmployerInfo Template { get; set; }
}
static class Program
{
    static void Main()
    {
        XmlSerializer serial = new XmlSerializer(typeof(Output));
        using (var reader = new StringReader(@"<Output>
    <Template recordID=""12"">
        <Employer type=""String"">
            <Value>Google</Value>
            <Value>GigaSoft inc.</Value>
        </Employer>
        <Designation  type=""String"">
            <Value>Google</Value>
        </Designation>
        <Duration  type=""String"" />
    </Template>
</Output>"))
        {
            EmployerInfo fooBar = ((Output)serial.Deserialize(reader)).Template;
        }
    }
}

Note also tat the type returned from XmlSerializer(typeof(Output))'s deserialize method is going to be a Output record.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文