1

我需要将我的 C# 类序列化为 XML,如下所示:

<request>
    <session>12345</session>
    <page>1</page>
    <elements_per_page>999</elements_per_page>
    <location>
       <zone>aaaa</zone>
       <region>bbbb</region>
       <coordinates>
           <lat>38.680632</lat>
           <lon>-96.5001</lon>
       </coordinates>
    </location>
</request> 

我不想要的是 3 个类(请求、位置、坐标),我只想要 1 个具有所有可更改属性的类作为该类的根,然后添加一些可以创建嵌套 XML 的序列化标签,这可能吗?

让我们从裸类开始:

[XmlRoot]
class request
{
    [XmlElement]
    public int session { get; set; }
    [XmlElement]
    public int page { get; set; }
    [XmlElement]
    public int elements_per_page { get; set; }
    [?]
    public string zone { get; set; }
    [?]
    public string region { get; set; }
    [?]
    public decimal lat { get; set; }
    [?]
    public decimal lon { get; set; }
} 

如何映射它们以便创建我描述的 XML?感谢您的帮助好人:)

4

1 回答 1

0

“这有可能吗?”

不,有 3 节课有什么问题?您想如何以其他方式创建 XML 到静态代码的链接?

此外,如果您只是想简单地吐出一些 XML,例如,您可以使用System.Xml.XmlDocument并从头开始构建一个 xml,您可以使用System.Xml.Serialization.XmlSerializer. 像这样:

public string SerializeRequest(Request request)
{
        XmlDocument document = new XmlDocument();

        XmlElement requestElement = document.CreateElement("request");

        XmlElement sessionElement = document.CreateElement("session");
        sessionElement.InnerText = request.session.ToString(CultureInfo.InvariantCulture);

        XmlElement pageElement = document.CreateElement("page");
        pageElement.InnerText = request.page.ToString(CultureInfo.InvariantCulture);

        XmlElement elementsPerPageElement = document.CreateElement("elements_per_page");
        elementsPerPageElement.InnerText = request.elements_per_page.ToString(CultureInfo.InvariantCulture);

        XmlElement zoneElement = document.CreateElement("zone");
        zoneElement.InnerText = request.zone;
        XmlElement regionElement = document.CreateElement("region");
        regionElement.InnerText = request.region;

        XmlElement latElement = document.CreateElement("lat");
        latElement.InnerText = request.lat.ToString(CultureInfo.InvariantCulture);
        XmlElement lonElement = document.CreateElement("lon");
        lonElement.InnerText = request.lon.ToString(CultureInfo.InvariantCulture);
        XmlElement coordinatesElement = document.CreateElement("coordinate");
        coordinatesElement.AppendChild(latElement);
        coordinatesElement.AppendChild(lonElement);

        XmlElement locationElement = document.CreateElement("location");
        locationElement.AppendChild(zoneElement);
        locationElement.AppendChild(regionElement);
        locationElement.AppendChild(coordinatesElement);

        requestElement.AppendChild(sessionElement);
        requestElement.AppendChild(pageElement);
        requestElement.AppendChild(elementsPerPageElement);
        requestElement.AppendChild(locationElement);

        document.AppendChild(requestElement);

        string serializedObj = document.OuterXml;
        return serializedObj;
}
于 2013-08-28T13:05:26.513 回答