本文介绍了需要通过使用 DataContractSerializer 来填充 XML 来拥有孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种方法可以将错误消息放入 1 xml 并将其发送给客户端.如果出现错误,错误可能有多个,我将返回 XMLErrMessage 中的列表 pf 错误.我想在评论中显示它们,但每个错误都显示为 1 个 xml 子项:

I have a method that put the error messages in 1 xml and send it to client. In case of error, errors can be several, I am returning the list pf errors that are in XMLErrMessage. I want to show them in comment but each error as 1 xml child:

  <comments>
     <comment>XMLErrMessage1</comment>
     <comment>XMLErrMessage2</comment>
    <comment>XMLErrMessage3</comment>
  </comments>

这是我的方法:

    public string ProcessXML(CommonLibrary.Model.TransferData dto, bool Authenticated)
    {
        DataContractSerializer dcs = new DataContractSerializer(typeof(CFCConnectResponse));
        MemoryStream ms = new MemoryStream();
        utility.utilities utl = new utility.utilities();
        List<string> XMLErrMessage =null;

        if (Authenticated)
        {
            if (!string.IsNullOrEmpty(dto.xml))
            {
                XMLErrMessage = utl.validateXML(dto.xml, xsdFilePath, currentSchema);

                if (XMLErrMessage.Count==1)
                {
                    dcs.WriteObject(ms, new CFCConnectResponse() { StatusCode = 101, StatusDescription = "Success" });
                    ms.Position = 0;
                }
                else
                {
                    dcs.WriteObject(ms, new CFCConnectResponse() { StatusCode = 201, StatusDescription = "XML Validation Fails", Comments=XMLErrMessage });
                    ms.Position = 0;
                }
            }
        }
        else
        {
            dcs.WriteObject(ms, new CFCConnectResponse() { StatusCode = 401, StatusDescription = "Authentication Fails" });
           // ms.Position = 0;
        }
        string s = new StreamReader(ms).ReadToEnd();  // xml result
        Console.WriteLine(s);
        return s;
    }

这是合同类:

public class CFCConnectResponse
{
    [DataMember]
    public int StatusCode;
    [DataMember]
    public string StatusDescription;
    [DataMember]
    public List<string> Comments;

推荐答案

CollectionDataContract 属性允许您控制集合元素名称,但是由于它只能针对一个类或结构,您必须创建一个List 的自定义子类和所需的合约,如下所示:

The CollectionDataContract attribute allows you to control the collection element names, however since it can only target a class or struct, you must create a custom subclass of List<T> with the desired contract, like so:

[DataContract(Namespace = "")]
[KnownType(typeof(CommentList))]
public class CFCConnectResponse
{
    [DataMember]
    public int StatusCode;
    [DataMember]
    public string StatusDescription;
    [DataMember(Name="comments")]
    public CommentList Comments;
}

[CollectionDataContract(ItemName = "comment", Namespace="")]
public class CommentList : List<string>
{
    public CommentList()
        : base()
    {
    }

    public CommentList(params string[] strings)
        : base(strings)
    {
    }

    public CommentList(IEnumerable<string> strings)
        : base(strings)
    {
    }
}

然后,测试:

public static class TestCFCConnectResponse
{
    static CFCConnectResponse CreateTest()
    {
        return new CFCConnectResponse()
        {
            StatusCode = 101,
            StatusDescription = "here is a description",
            Comments = new CommentList("XMLErrMessage1", "XMLErrMessage2", "XMLErrMessage3"),
        };
    }

    public static void Test()
    {
        var response = CreateTest();

        try
        {
            var xml = DataContractSerializerHelper.GetXml(response);
            Debug.Write(xml);
            var newResponse = DataContractSerializerHelper.GetObject<CFCConnectResponse>(xml);
            Debug.Assert(newResponse != null);
            Debug.Assert(response.StatusCode == newResponse.StatusCode);
            Debug.Assert(response.StatusDescription == newResponse.StatusDescription);
            Debug.Assert(newResponse.Comments.SequenceEqual(response.Comments));
        }
        catch (Exception ex)
        {
            Debug.Assert(false, ex.ToString());
        }
    }
}

这会产生以下输出,没有断言:

This produces the following output, with no asserts:

<?xml version="1.0" encoding="utf-16"?>
<CFCConnectResponse xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <StatusCode>101</StatusCode>
    <StatusDescription>here is a description</StatusDescription>
    <comments>
        <comment>XMLErrMessage1</comment>
        <comment>XMLErrMessage2</comment>
        <comment>XMLErrMessage3</comment>
    </comments>
</CFCConnectResponse>

更新

如果更改 CFCConnectResponse.CommentList 以获得 &设置 CommentList 需要对遗留代码进行太多更改,您可以执行以下操作:

If changing CFCConnectResponse.CommentList to get & set a CommentList requires too many changes to legacy code, you can do the following:

[DataContract(Namespace = "")]
[KnownType(typeof(CommentList))]
public class CFCConnectResponse
{
    [DataMember]
    public int StatusCode;

    [DataMember]
    public string StatusDescription;

    [IgnoreDataMember]
    public List<string> Comments { get; set; }

    [DataMember(Name = "comments")]
    private CommentList SerializableComments
    {
        get
        {
            return new CommentList(Comments);
        }
        set
        {
            Comments = value.ToList();
        }
    }
}

这将保留 List;序列化 & 时的 Comments 属性反序列化 CommentList.

This preserves the List<string> Comments property while serializing & deserializing a CommentList.

这篇关于需要通过使用 DataContractSerializer 来填充 XML 来拥有孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 01:30