带有键值的XML序列化节点名称

时间:2016-04-29 05:53:44

标签: c# xml wcf serialization xml-serialization

我想将我的代码序列化为XML。

现在我有:

  var exist = true;

        function checkAccountSuspension() {
            var accountNumberId = $('#DepositAccountNumberIdHiddenField').val();
            if (accountNumberId == "") {
                //
            } else {
                try {
                    var url = '/WebMethods/AccountDetails.asmx/IsAccountSuspended';
                    var d = { accountNumberId: accountNumberId };
                    //$.post(url, d, function (data) {
                    var jqXhr = ajaxPost(url, d, true);
                    jqXhr.done(function (data) {
                        var ret = data.d;
                        if (!ret) {
                            $('#DepositAccountNumberIdHiddenField').val(accountNumberId);
                            exist = true;
                        }
                        else {
                            //$('#DepositAccountNumberIdHiddenField').val('');
                            //  bootbox.alert("<b class='text-red'>Suspended Account</b> <br/>This Account has been Suspended.");
                            swal({
                                title: "Suspended Account!",
                                text: "This Account is Suspended",
                                type: "error",
                                confirmButtonText: "OK",
                                imageSize: "20x20"
                            });
                            exist = false;
                            resetAllInputs();
                        }
                    }).fail(function (ex) {
                        bootbox.alert("Requested process fail to execute");
                        //$('#DepositAccountNumberIdHiddenField').val('');
                    });
                }
                catch (e) {
                    bootbox.alert('Error: ' + e);
                }

            }
        }

我的C#代码如下:

<?xml version="1.0" encoding="utf-8"?>
<SerializationClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Value>Test</Value>
</SerializationClass>

但我想要像XML一样:

public class SerializationClass
{
[XmlElement("Value")]
public string City { get; set; }
}

2 个答案:

答案 0 :(得分:1)

您无法使用您拥有的类结构(无需手动编码)。如果您愿意更改结构,这将有效:

public class SerializationClass
{
    public City City { get; set; }
}

public class City
{
    [XmlAttribute("value")]
    public string Value { get; set; }
}

答案 1 :(得分:0)

这种做法怎么样?

public class SerializationClass
{
    [XmlIgnore]
    public string City { get; set; }

    [EditorBrowsable(EditorBrowsableState.Never)]
    [XmlAnyElement]
    public XElement _City
    {
        get { return new XElement("City", new XAttribute("value", City)); }
        set { City = value.FirstAttribute.Value; }
    }
}