簡體   English   中英

有沒有一種方法可以從字符串創建SyndicationFeed?

[英]Is there a way to create a SyndicationFeed from a String?

我正在嘗試從已本地存儲的XML數據重新創建SyndicationFeed對象(System.ServiceModel.Syndication)。

如果我使用XMLDocument,這會很容易,我會調用LoadXml(string)。

SyndicationFeed將僅從XMLReader加載。 XMLReader僅將使用Stream或另一個XMLReader或TextReader。

由於XMLDocument將加載字符串,因此我嘗試按以下方式進行操作(以擴展方法的形式):

    public static SyndicationFeed ToSyndicationFeed(this XmlDocument document)
    {
        Stream thestream = Stream.Null;
        XmlWriter thewriter = XmlWriter.Create(thestream);

        document.WriteTo(thewriter);

        thewriter.Flush();
        XmlReader thereader = XmlReader.Create(thestream);

        SyndicationFeed thefeed = SyndicationFeed.Load(thereader);

        return thefeed;
    }

我無法使它正常工作。 即使XMLDocument填充了要加載到SyndicationFeed中的Feed,Stream始終為空。

您可以提供的任何幫助或指示將最有幫助。

謝謝羅伯托

由於StringReader擴展了TextReader,因此應該可以:

TextReader tr = new StringReader(xmlString);
XmlReader xmlReader = XmlReader.Create(tr);
SyndicationFeed feed = SyndicationFeed.Load(xmlReader);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM