试图读取xml文件[attributes]

时间:2013-06-21 18:53:42

标签: c# xml xml-deserialization

我在下面有这个xml: 我怎样才能反序化呢?我已经尝试过使用数据集并获取属性。


<?xml version="1.0" encoding="UTF-8"?>
<Result xmlns="urn:buscape" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" totalLooseOffers="0" schk="true" page="1" totalPages="1" totalResultsSellers="2" totalResultsReturned="2" totalResultsAvailable="2" xsi:schemaLocation="http://developer.buscape.com/admin/buscape.xsd">
    <details>
        <applicationID>3833764c71496937647a773d</applicationID>
        <date>2013-06-21T15:03:08.887-03:00</date>
        <elapsedTime>450</elapsedTime>
        <status>success</status>
        <code>0</code>
        <message>success</message>
    </details>
    <category concatenateCategoryName="false" hasOffer="true" isFinal="true" parentCategoryId="0" id="2921">
        <thumbnail url="http://imagem.buscape.com.br/bp5/categorias/2921.jpg" />
        <links>
            <link type="category" url="http://compare.buscape.com.br/cds.html?mdsrc=9262544&amp;mdapp=4657&amp;mddtn=51922611" />
            <link type="xml" url="http://sandbox.buscape.com/service/findOfferList/3833764c71496937647a773d/br/?categoryId=2921&amp;keyword=thalles+roberto&amp;sourceId=9262544" />
        </links>
        <name>CDs</name>
    </category>
    <offer id="119167238" categoryId="2921">
        <offerName>Uma História Escrita Pelo Dedo de Deus - Vol. 1 - Digipack - Thalles Roberto - Cod: 4049084 ( CD )</offerName>
        <offerShortName>Uma História</offerShortName>
        <links>
            <link type="offer" url="http://tracker.lomadee.com/tr/rd?b=amUMaQoBZRo4Jz8HbB9rPQQmLDcoZxoRCwE0Gzlzc2VnbGhsa2pqZWpoa2lla29nbGxob29lNioqLmRxcT0xMy4_LDtwPCstPT8uO3A9MTNwPCxxLiwxPQErMDc9MWE3OmNsZ2xveDM6LSw9Y2dsaGxramp4Mzo6KjBja29nbGxob294Mzo_Li5jamhraXg1KWMqNj8yMjstdSwxPDssKjFlMDEsOzhlb2UcDGVlZW5lbmVuZW5lZW9taW9mbWlpZmdtbWhlZW5lbmVuZW5lb29nb2hpbG1mZW5wbmVucG5lb2Vlbg--" />
        </links>
        <thumbnail url="http://thumbs.buscape.com.br/T100x100/__2.157-71a5906.jpg" />
        <price>
            <currency abbreviation="BRL" />
            <value>27.06</value>
            <parcel>
                <value>13.95</value>
                <number>2</number>
                <interest>2.06</interest>
            </parcel>
        </price>
        <seller oneClickBuyValue="0" oneClickBuy="false" advertiserId="0" pagamentoDigital="false" isTrustedStore="true" id="157">
            <sellerName>Siciliano</sellerName>
            <thumbnail url="http://imagem.buscape.com.br/vitrine/logo157.gif" />
            <links>
                <link type="seller" url="http://www.siciliano.com.br/default.asp?parc=BULL&amp;utm_source=buscape&amp;utm_medium=buscape&amp;utm_campaign=buscape" />
            </links>
            <contacts>
                <contact label="SAC" value="11-39334020" />
                <contact label="Televendas" value="03007893649" />
                <contact label="Televendas" value="11-36494747" />
            </contacts>
            <rating>
                <userAverageRating>
                    <numComments>12177</numComments>
                    <rating>7.0</rating>
                </userAverageRating>
                <eBitRating>
                    <numComments>12177</numComments>
                    <rating>sob-avaliação</rating>
                    <ratingNew>e-bit Loja Nova</ratingNew>
                    <ratingId>5</ratingId>
                </eBitRating>
            </rating>
        </seller>
    </offer>
</Result>

我需要来自缩略图的网址和来自链接的网址(具有优惠类型),内部。 我怎样才能在c#中获取这些数据?我有一个带有xml代码的字符串(它是动态的)。

1 个答案:

答案 0 :(得分:0)

这适用于序列化/反序列化xml

/// <summary>
/// Serializes an object.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="serializableObject"></param>
/// <param name="fileName"></param>
public void SerializeObject<T>(T serializableObject, string fileName)
{
    if (serializableObject == null) { return; }

    try
    {
        XmlDocument xmlDocument = new XmlDocument();
        XmlSerializer serializer = new XmlSerializer(serializableObject.GetType());
        using (MemoryStream stream = new MemoryStream())
        {
            serializer.Serialize(stream, serializableObject);
            stream.Position = 0;
            xmlDocument.Load(stream);
            xmlDocument.Save(fileName);
            stream.Close();
        }
    }
    catch (Exception ex)
    {
        //Log exception here
    }
}


/// <summary>
/// Deserializes an xml file into an object list
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="fileName"></param>
/// <returns></returns>
public T DeSerializeObject<T>(string fileName)
{
    if (string.IsNullOrEmpty(fileName)) { return default(T); }

    T objectOut = default(T);

    try
    {
        string attributeXml = string.Empty;

        XmlDocument xmlDocument = new XmlDocument();
        xmlDocument.Load(fileName);
        string xmlString = xmlDocument.OuterXml;

        using (StringReader read = new StringReader(xmlString))
        {
            Type outType = typeof(T);

            XmlSerializer serializer = new XmlSerializer(outType);
            using (XmlReader reader = new XmlTextReader(read))
            {
                objectOut = (T)serializer.Deserialize(reader);
                reader.Close();
            }

            read.Close();
        }
    }
    catch (Exception ex)
    {
        //Log exception here
    }

    return objectOut;
}