首页 > 解决方案 > 使用 WebClient 从 xml 反序列化到 C# 中的对象时出现意外字符

问题描述

我从微软执行 url:像这样

我得到的结果如下:

<SuggestResponse xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Query>state of decay 2</Query>
    <ResultSets>
        <SuggestResults>
            <Source>dcatall-products</Source>
            <FromCache>true</FromCache>
            <Type>product</Type>
            <Suggests>
                <Product>
                    <Curated>false</Curated>
                    <Source>Games</Source>
                    <Title>State of Decay 2</Title>
                    <Url>//www.microsoft.com/en-us/store/p/state-of-decay-2/9nt4x7p8b9nb</Url>
                    <ImageUrl>//store-images.s-microsoft.com/image/apps.30042.14425140369408817.aa13e205-dc3a-4b1f-b9db-3cc6eda9b9ef.94017af1-0966-4275-810b-fe47f11c81d0?w=150&amp;h=150</ImageUrl>
                    <Metas>
                        <Meta>
                            <Key>BigCatalogId</Key>
                            <Value>9NT4X7P8B9NB</Value>
                        </Meta>
                        <Meta>
                            <Key>ProductType</Key>
                            <Value>Game</Value>
                        </Meta>
                        <Meta>
                            <Key>Icon</Key>
                            <Value>//store-images.s-microsoft.com/image/apps.30042.14425140369408817.aa13e205-dc3a-4b1f-b9db-3cc6eda9b9ef.94017af1-0966-4275-810b-fe47f11c81d0</Value>
                        </Meta>
                        <Meta>
                            <Key>ImageHeight</Key>
                            <Value>150</Value>
                        </Meta>
                        <Meta>
                            <Key>ImageWidth</Key>
                            <Value>150</Value>
                        </Meta>
                        <Meta>
                            <Key>ImageType</Key>
                            <Value>Tile</Value>
                        </Meta>
                        <Meta>
                            <Key>AppBgColor</Key>
                            <Value>#ca6929</Value>
                        </Meta>
                    </Metas>
                </Product>
                <Product>
                    <Curated>false</Curated>
                    <Source>Games</Source>
                    <Title>State Of Decay 2: Ultimate Edition</Title>
                    <Url>//www.microsoft.com/en-us/store/p/state-of-decay-2-ultimate-edition/9nsch45hz9hk</Url>
                    <ImageUrl>//store-images.s-microsoft.com/image/apps.16619.14365785075596216.1152921504746356298.788f6ae8-57e0-4a3f-8bda-6c2d5e4d546c?w=150&amp;h=150</ImageUrl>
                    <Metas>
                        <Meta>
                            <Key>BigCatalogId</Key>
                            <Value>9NSCH45HZ9HK</Value>
                        </Meta>
                        <Meta>
                            <Key>ProductType</Key>
                            <Value>Game</Value>
                        </Meta>
                        <Meta>
                            <Key>Icon</Key>
                            <Value>//store-images.s-microsoft.com/image/apps.16619.14365785075596216.1152921504746356298.788f6ae8-57e0-4a3f-8bda-6c2d5e4d546c</Value>
                        </Meta>
                        <Meta>
                            <Key>ImageHeight</Key>
                            <Value>2160</Value>
                        </Meta>
                        <Meta>
                            <Key>ImageWidth</Key>
                            <Value>2160</Value>
                        </Meta>
                        <Meta>
                            <Key>ImageType</Key>
                            <Value>BoxArt</Value>
                        </Meta>
                        <Meta>
                            <Key>AppBgColor</Key>
                            <Value>#FFFFFF</Value>
                        </Meta>
                    </Metas>
                </Product>
                <Product>
                    <Curated>false</Curated>
                    <Source>Devices</Source>
                    <Title>State of Decay 2 for Xbox One</Title>
                    <Url>//www.microsoft.com/en-us/store/p/state-of-decay-2-for-xbox-one/8p2fgp3bf0fc</Url>
                    <ImageUrl>//img-prod-cms-rt-microsoft-com.akamaized.net/cms/api/am/imageFileData/RE1Tc21?ver=3d93&amp;w=272&amp;h=223&amp;o=f&amp;m=6&amp;f=jpg</ImageUrl>
                    <Metas>
                        <Meta>
                            <Key>BigCatalogId</Key>
                            <Value>8P2FGP3BF0FC</Value>
                        </Meta>
                        <Meta>
                            <Key>ProductType</Key>
                            <Value>Devices</Value>
                        </Meta>
                        <Meta>
                            <Key>Icon</Key>
                            <Value>https://img-prod-cms-rt-microsoft-com.akamaized.net/cms/api/am/imageFileData/RE1Tc21?ver=3d93</Value>
                        </Meta>
                        <Meta>
                            <Key>ImageHeight</Key>
                            <Value>1540</Value>
                        </Meta>
                        <Meta>
                            <Key>ImageWidth</Key>
                            <Value>1189</Value>
                        </Meta>
                        <Meta>
                            <Key>ImageType</Key>
                            <Value>tile</Value>
                        </Meta>
                    </Metas>
                </Product>
            </Suggests>
        </SuggestResults>
        <SuggestResults>
            <Source>microsoft-terms</Source>
            <FromCache>false</FromCache>
            <Type>term</Type>
            <Suggests>
                <Term>
                    <Curated>false</Curated>
                    <Txt>state of decay year one survival edition</Txt>
                </Term>
                <Term>
                    <Curated>false</Curated>
                    <Txt>state of decay year one survival edition for xbox one</Txt>
                </Term>
                <Term>
                    <Curated>false</Curated>
                    <Txt>state of decay for xbox 360 download code</Txt>
                </Term>
            </Suggests>
        </SuggestResults>
    </ResultSets>
    <ErrorSets>
        <SuggestErrorSet>
            <Source>iris-products</Source>
            <Message>Code: 2040. Message: Demand source returns error (Name: GN_ps, Error: No eligible content.).</Message>
        </SuggestErrorSet>
    </ErrorSets>
</SuggestResponse>

我已经粘贴了结果 xml(在 Visual Studio 中,我选择了 Edit -> Paste special -> Paste XML as classes)...

使用 Newtonsoft Json 反序列化时

var data = JsonConvert.DeserializeObject<MsQueryXmlDetailResponse.SuggestResponse>(response);

我有:

解析值时遇到意外字符:<。路径'',第 0 行,位置 0。'

我也试过

var data = JsonConvert.DeserializeObject<MsQueryXmlDetailResponse>(response);

但同样的事情...

标签: c#

解决方案


推荐阅读