在作为列表父元素的元素中添加属性

发布于 2024-12-12 04:40:03 字数 2367 浏览 2 评论 0原文

我正在尝试序列化一个对象,但面临一些有关包含数组的父元素的属性的问题。

我有以下 xml 结构,但无法在 RatePlans 元素中添加该属性。

<Root>
<RatePlans Attribute="??this one??">
    <RatePlan Attribute1="RPC" Attribute2="MC" Attribute3="RPT">
        .
        .
        .
    </RatePlan>
    <RatePlan Attribute1="RPC2" Attribute2="MC3" Attribute3="RPT4">
        .
        .
        .
    </RatePlan>
</RatePlans>
</Root>

这就是我到目前为止所做的:

namespace XmlT {
    [Serializable]
    [XmlRoot("Root")]
    public class Root {
        public List<RatePlan> RatePlans { get; set; }
    }
}

namespace XmlT {
[Serializable]
public class RatePlan {


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

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

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

这给了我一个正确的结构,但我不知道如何添加我想要的属性

另一种方法

我也尝试了另一种方法,但这给了我错误的值根本不。

namespace XmlT {
    [Serializable]
    [XmlRoot("Root")]
    public class Root {
        public RatePlans RatePlans { get; set; }
    }
}

namespace XmlT {
[Serializable]
public class RatePlans {

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

    public List<RatePlan> RatePlan { get; set; }
}
}

编辑

这是我用于序列化的方法

protected static string Serialize<T>(object objToXml, bool IncludeNameSpace = false) where T : class {
        StreamWriter stWriter = null;
        XmlSerializer xmlSerializer;
        string buffer;
        try {
            xmlSerializer = new XmlSerializer(typeof(T));
            MemoryStream memStream = new MemoryStream();
            stWriter = new StreamWriter(memStream);
            if (!IncludeNameSpace) {

                var xs = new XmlSerializerNamespaces();

                xs.Add("", "");
                xmlSerializer.Serialize(stWriter, objToXml, xs);
            } else {
                xmlSerializer.Serialize(stWriter, objToXml);
            }
            buffer = Encoding.ASCII.GetString(memStream.GetBuffer());
        } catch (Exception Ex) {
            throw Ex;
        } finally {
            if (stWriter != null) stWriter.Close();
        }
        return buffer;
    }

有人知道我该怎么做吗?

谢谢

I am trying to serialize an object but I am facing some issues regarding the attributes of a parent element that contains an array.

I have the following xml structure and I can't add the attribute in RatePlans element.

<Root>
<RatePlans Attribute="??this one??">
    <RatePlan Attribute1="RPC" Attribute2="MC" Attribute3="RPT">
        .
        .
        .
    </RatePlan>
    <RatePlan Attribute1="RPC2" Attribute2="MC3" Attribute3="RPT4">
        .
        .
        .
    </RatePlan>
</RatePlans>
</Root>

This is what I have done so far:

namespace XmlT {
    [Serializable]
    [XmlRoot("Root")]
    public class Root {
        public List<RatePlan> RatePlans { get; set; }
    }
}

namespace XmlT {
[Serializable]
public class RatePlan {


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

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

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

This gives me a correct structure but I don't know how to add the attribute I want

Another approach

I've tried also another approach but this gives me wrong values at all.

namespace XmlT {
    [Serializable]
    [XmlRoot("Root")]
    public class Root {
        public RatePlans RatePlans { get; set; }
    }
}

namespace XmlT {
[Serializable]
public class RatePlans {

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

    public List<RatePlan> RatePlan { get; set; }
}
}

EDIT

this the method that I am using for the serialization

protected static string Serialize<T>(object objToXml, bool IncludeNameSpace = false) where T : class {
        StreamWriter stWriter = null;
        XmlSerializer xmlSerializer;
        string buffer;
        try {
            xmlSerializer = new XmlSerializer(typeof(T));
            MemoryStream memStream = new MemoryStream();
            stWriter = new StreamWriter(memStream);
            if (!IncludeNameSpace) {

                var xs = new XmlSerializerNamespaces();

                xs.Add("", "");
                xmlSerializer.Serialize(stWriter, objToXml, xs);
            } else {
                xmlSerializer.Serialize(stWriter, objToXml);
            }
            buffer = Encoding.ASCII.GetString(memStream.GetBuffer());
        } catch (Exception Ex) {
            throw Ex;
        } finally {
            if (stWriter != null) stWriter.Close();
        }
        return buffer;
    }

Does anyone know how could I do this?

Thanks

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

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

发布评论

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

评论(1

早乙女 2024-12-19 04:40:03

如果第二个示例中的 RatePlans 类继承自 List,您将获得所需的结果:

[Serializable]
public class RatePlans: List<RatePlan>
{
    [XmlAttribute]
    public string HotelCode { get; set; }
}

<强>编辑:

我的错。从集合继承的类的字段不会被序列化。我不知道这一点。抱歉...

但是,这个解决方案有效:

[Serializable]
[XmlRoot("Root")]
public class Root 
{
    public RatePlans RatePlans { get; set; }
}


[Serializable]
public class RatePlans
{
    [XmlAttribute]
    public string HotelCode { get; set; }

    [XmlElement("RatePlan")]
    public List<RatePlan> Items = new List<RatePlan>();
}

If the RatePlans class from the 2nd example inherits from List<RatePlan> you will get the desired result:

[Serializable]
public class RatePlans: List<RatePlan>
{
    [XmlAttribute]
    public string HotelCode { get; set; }
}

Edit:

My bad. Fields of classes inheriting from collections will not be serialized. I didn't knew that. Sorry...

However, this solution works:

[Serializable]
[XmlRoot("Root")]
public class Root 
{
    public RatePlans RatePlans { get; set; }
}


[Serializable]
public class RatePlans
{
    [XmlAttribute]
    public string HotelCode { get; set; }

    [XmlElement("RatePlan")]
    public List<RatePlan> Items = new List<RatePlan>();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文