第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

將響應(yīng)流轉(zhuǎn)換為 XML

將響應(yīng)流轉(zhuǎn)換為 XML

C#
Qyouu 2023-08-20 09:56:54
我向演示 API 發(fā)送了一個(gè) XML 帖子,響應(yīng)以 XML 流的形式返回,如下所示:API=3CProductData&XML=%3CProductData+Name%3D%22NameTest%22%3E%0D%0A++%3CId%3EXXXXXXXXX%3C%2FId%3E%0D%0A%3C%2FProductData%3E我猜這就是流的樣子,我的目標(biāo)是獲取該響應(yīng)并將其存儲(chǔ)在新的 ProductData 對(duì)象中,這是我迄今為止所做的:    HttpWebResponse response = (HttpWebResponse)request.GetResponse();    if (response.StatusCode == HttpStatusCode.OK)    {        // as an xml: deserialise into your own object or parse as you wish        StreamReader respStream = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default);        string receivedResponse = respStream.ReadToEnd();        XmlSerializer x = new XmlSerializer(typeof(ProductData));        ProductData product = (ProductData) x.Deserialize(new StringReader(receivedResponse));        Console.WriteLine("Node1: " + product.Id.ToString());        Console.WriteLine("Node2: " + product.Name);        Console.ReadKey();    }錯(cuò)誤返回時(shí)出現(xiàn) System.InvalidOperationException:“XML 文檔 (0, 0) 中存在錯(cuò)誤?!?XmlException:缺少根元素。
查看完整描述

2 回答

?
慕哥6287543

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超10個(gè)贊

這里有兩種不同的解決方案。


    public ProductData TestFunction()

    {

        ProductData result = new ProductData();


        string apiResponse = "API=3CProductData&XML=%3CProductData+Name%3D%22NameTest%22%3E%0D%0A++%3CId%3EXXXXXXXXX%3C%2FId%3E%0D%0A%3C%2FProductData%3E";

        string xml = HttpUtility.UrlDecode(apiResponse.Substring(apiResponse.IndexOf("XML=") + 4));

        XmlDocument document = new XmlDocument();

        document.LoadXml(xml);


        XmlNode newNode = document.DocumentElement;


        // Name is actually an attribute on the ProductData

        result.Name = ((XmlAttribute)newNode.Attributes["Name"]).InnerText;


        // Id is an actual node

        result.ID = ((XmlNode)newNode.FirstChild).InnerText;



        using (TextReader reader = new StringReader(xml))

        {

            var serializer = new XmlSerializer(typeof(ProductData));

            result = (ProductData)serializer.Deserialize(reader);

        }


        return result;

    }


[Serializable]

[XmlRoot("ProductData")]

public class ProductData

{

    [XmlElement("Id")]

    public string ID { get; set; }


    [XmlAttribute("Name")]

    public string Name { get; set; }


}

這段代碼有一個(gè)非常脆弱的部分,我沒有花很多時(shí)間嘗試處理它。在我看來(lái),XML 的格式并不是很好,因此您必須在 后面添加子字符串,XML=這就是我在末尾添加 +4 的原因??赡苁且环N更順利的方法,但問(wèn)題仍然在于轉(zhuǎn)換 XML。由于 XML 非常簡(jiǎn)單,您只需通過(guò) SelectSingleNode 定位值即可。如果您想采用 StreamReader 路線,則需要確保您的類/屬性已設(shè)置屬性(即 [XmlRoot("Productdata")])


查看完整回答
反對(duì) 回復(fù) 2023-08-20
?
嗶嗶one

TA貢獻(xiàn)1854條經(jīng)驗(yàn) 獲得超8個(gè)贊

API=3CProductData&XML=您必須刪除字符串中的部分,然后解碼您的部分 XML


看看這段代碼的工作原理:


string strRegex = @"<ProductData Name=""NameTest"">\r\n? <Id>XXXXXXXXX</Id>\r\n</ProductData>";

ProductData result = null;

using (TextReader reader = new StringReader(strRegex))

{

? ? var serializer = new XmlSerializer(typeof(ProductData));

? ? result = (ProductData)serializer.Deserialize(reader);

}


查看完整回答
反對(duì) 回復(fù) 2023-08-20
  • 2 回答
  • 0 關(guān)注
  • 162 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)