Problem with AutoMapper and displaying IEnumerable collections

Here is the code for the translator:

public IEnumerable<GetQuestionsContract> Map(IEnumerable<XmlNode> nodes, XmlNamespaceManager namespaceManager)
    {
        Mapper.CreateMap<XmlNode, GetQuestionsContract>()
            .ForMember(
                dest => dest.Id, 
                options =>
                options.ResolveUsing<XmlNodeResolver<string>>().FromMember(
                    source => source.SelectSingleNode("//wfauth60xsd:questionID", namespaceManager)))
            .ForMember(
                dest => dest.Question, 
                options =>
                options.ResolveUsing<XmlNodeResolver<string>>().FromMember(
                    source => source.SelectSingleNode("//wfauth60xsd:question", namespaceManager)));

        return Mapper.Map<IEnumerable<XmlNode>, List<GetQuestionsContract>>(nodes);
    }

While this works, only one returned element appears in the IEnumerable list several times (as many times as there are elements in the XmlNodeList).

Update . I simplified the code and updated the title. The script works fine if I map one to XmlNode, but enumeration seems to be the problem. For example, the following code works very well:

public SomeIdContract Map(XmlDocument document, XmlNamespaceManager namespaceManager)
    {
        Mapper.CreateMap<XmlDocument, SomeIdContract>()
            .ForMember(
                dest => dest.Id, 
                options =>
                options.ResolveUsing<XmlNodeResolver<string>>().FromMember(
                    source => source.SelectSingleNode("//wfauth60msgs:someID", namespaceManager)));

        return Mapper.Map<XmlDocument, SomeIdContract>(document);
    }

Any thoughts? Thank!

+3
source share
2 answers

It seems that the problem is not with AutoMapper, but with XPath, which selects the first node for some crazy reason.

XmlDocument XDocument Linq-to-Xml .

.

+1

. , AutoMapper Id (Guid) ( nhiberate) ( , ). , , , IEnumerable . Id , Id , .

+3

Source: https://habr.com/ru/post/1763154/


All Articles