-3

I saw this question already, but I didnt see an answer..

So I get this error:

The ':' character, hexadecimal value 0x3A, cannot be included in a name.

On this code:

    XDocument XMLFeed = XDocument.Load("http://feeds.foxnews.com/foxnews/most-popular?format=xml");
    XNamespace content = "http://purl.org/rss/1.0/modules/content/";

    var feeds = from feed in XMLFeed.Descendants("item")
        select new
        {
            Title = feed.Element("title").Value,
            Link = feed.Element("link").Value,
            pubDate = feed.Element("pubDate").Value,
            Description = feed.Element("description").Value,
            MediaContent = feed.Element(content + "encoded")
        };

    foreach (var f in feeds.Reverse())
    {
        ....
    }

An item looks like that:

<rss>    
<channel>

....items....

<item>
<title>Pentagon confirms plan to create new spy agency</title>
<link>http://feeds.foxnews.com/~r/foxnews/most-popular/~3/lVUZwCdjVsc/</link>
<category>politics</category>
<dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/" />
<pubDate>Tue, 24 Apr 2012 12:44:51 PDT</pubDate>
<guid isPermaLink="false">http://www.foxnews.com/politics/2012/04/24/pentagon-confirms-plan-to-create-new-spy-agency/</guid>
<content:encoded xmlns:content="http://purl.org/rss/1.0/modules/content/"><![CDATA[|http://global.fncstatic.com/static/managed/img/Politics/panetta_hearing_030712.jpg<img src="http://feeds.feedburner.com/~r/foxnews/most-popular/~4/lVUZwCdjVsc" height="1" width="1"/>]]></content:encoded>
<description>The Pentagon confirmed Tuesday that it is carving out a brand new spy agency expected to include several hundred officers focused on intelligence gathering around the world.&amp;amp;#160;</description>
<dc:date xmlns:dc="http://purl.org/dc/elements/1.1/">2012-04-4T19:44:51Z</dc:date>
<feedburner:origLink>http://www.foxnews.com/politics/2012/04/24/pentagon-confirms-plan-to-create-new-spy-agency/</feedburner:origLink>
</item>

....items....

</channel>
</rss>    

All I want is to get the "http://global.fncstatic.com/static/managed/img/Politics/panetta_hearing_030712.jpg", and before that check if content:encoded exists..

Thanks.

EDIT: I've found a sample that I can show and edit the code that tries to handle it..

EDIT2: I've done it in the ugly way:

text.Replace("content:encoded", "contentt").Replace("xmlns:content=\"http://purl.org/rss/1.0/modules/content/\"","");

and then get the element in the normal way:

MediaContent = feed.Element("contentt").Value
4

2 に答える 2

0

XNamespace を使用する必要があります。

XNamespace content = "...";

// later in your code ...
MediaContent = feed.Element(content + "encoded")

詳細はこちらをご覧ください。

(もちろん、コンテンツに割り当てる文字列は と同じですxmlns:content="...")。

于 2012-04-25T11:55:59.147 に答える
0

次のコード

    static void Main(string[] args)
    {

            var XMLFeed = XDocument.Parse(
@"<rss>    
<channel>

....items....

<item>
<title>Pentagon confirms plan to create new spy agency</title>
<link>http://feeds.foxnews.com/~r/foxnews/most-popular/~3/lVUZwCdjVsc/</link>
<category>politics</category>
<dc:creator xmlns:dc='http://purl.org/dc/elements/1.1/' />
<pubDate>Tue, 24 Apr 2012 12:44:51 PDT</pubDate>
<guid isPermaLink='false'>http://www.foxnews.com/politics/2012/04/24/pentagon-confirms-plan-to-create-new-spy-agency/</guid>
<content:encoded xmlns:content='http://purl.org/rss/1.0/modules/content/'><![CDATA[|http://global.fncstatic.com/static/managed/img/Politics/panetta_hearing_030712.jpg<img src='http://feeds.feedburner.com/~r/foxnews/most-popular/~4/lVUZwCdjVsc' height='1' width='1'/>]]></content:encoded>
<description>The Pentagon confirmed Tuesday that it is carving out a brand new spy agency expected to include several hundred officers focused on intelligence gathering around the world.&amp;amp;#160;</description>
<dc:date xmlns:dc='http://purl.org/dc/elements/1.1/'>2012-04-4T19:44:51Z</dc:date>
<!-- <feedburner:origLink>http://www.foxnews.com/politics/2012/04/24/pentagon-confirms-plan-to-create-new-spy-agency/</feedburner:origLink> -->
</item>

....items....

</channel>
</rss>");
            XNamespace contentNs = "http://purl.org/rss/1.0/modules/content/";
            var feeds = from feed in XMLFeed.Descendants("item")
                        select new
                                   {
                                       Title = (string)feed.Element("title"),
                                       Link = (string)feed.Element("link"),
                                       pubDate = (string)feed.Element("pubDate"),
                                       Description = (string)feed.Element("description"),
                                       MediaContent = GetMediaContent((string)feed.Element(contentNs + "encoded"))
                                   };
            foreach(var item in feeds)
            {
                Console.WriteLine(item);
            }
        }

        private static string GetMediaContent(string content)
        {
            int imgStartPos = content.IndexOf("<img");
            if(imgStartPos > 0)
            {
                int startPos = content[0] == '|' ? 1 : 0;

                return content.Substring(startPos, imgStartPos - startPos);
            }

            return string.Empty;
        }

結果:

{ タイトル = ペンタゴンは、新しいスパイ機関を作成する計画を確認します。リンク = http://feeds.f
oxnews.com/~r/foxnews/most-popular/~3/lVUZwCdjVsc/, pubDate = Tue, 24 Apr 2012 1
2:44:51 PDT、説明 = 国防総省は火曜日に、それが切り分けていることを確認しました
 に焦点を当てた数百人の警官を含むと予想される真新しいスパイ機関
世界中で情報収集. , MediaContent = http://global
.fncstatic.com/static/managed/img/Politics/panetta_hearing_030712.jpg }
何かキーを押すと続行します 。. .

いくつかのポイント:

  • Xmlをテキストとして扱いたくない - あなたのケースでは名前空間宣言を削除しましたが、実際には名前空間がインラインで宣言された場合(つまり、プレフィックスにバインドせずに)、または別のプレフィックスが定義された場合、コードは意味的に両方のドキュメントであっても機能しません同等だろう
  • CDATA の内容とその扱い方を知らない限り、常にテキストとして扱いたいと思うでしょう。それが別のものであることがわかっている場合は、解析後に別の方法で処理できます - 詳細については、以下の CDATA に関する私の詳細を参照してください
  • 要素が欠落している場合に NullReferenceExceptions を回避するために、.Value を呼び出す代わりに明示的な変換演算子 (文字列) を使用しました。
  • 投稿した Xml は有効な xml ではありませんでした - feedburner プレフィックスの名前空間 Uri がありませんでした

これはもはや問題とは関係ありませんが、一部の人々にとって役立つ可能性があるため、残します

エンコード要素の内容が考慮される限り、それは CDATA セクション内にあります。CDATA セクションの中身は Xml ではなくプレーンテキストです。CDATA は通常、「<」、「>」、「&」文字をエンコードする必要がないために使用されます (CDATA がなければ、Xml ドキュメント自体を壊さないように < > および & としてエンコードする必要があります) が、Xml プロセッサは文字を次のように扱います。それらがエンコードされているかのように CDATA を (または、より正確にはそれらをエンコードします)。CDATA は、HTML を埋め込む場合に便利です。なぜなら、埋め込まれたコンテンツはテキスト的には元のコンテンツのように見えますが、HTML が整形式の Xml でない場合でも、XML を壊すことはないからです。CDATA コンテンツは Xml ではなくテキストであるため、Xml として扱うことはできません。おそらく、これをテキストとして扱い、たとえば正規表現を使用する必要があります。これは、コンテンツを XElement に再度ロードして処理できる有効な Xml です。あなたの場合、コンテンツが混在しているため、少し汚いハックを使用しない限り、簡単ではありません。混合コンテンツではなく、最上位の要素が 1 つだけあれば、すべてが簡単になります。ハックは、すべての面倒を避けるために要素を追加することです。foreach ルック内では、次のようなことができます。

var mediaContentXml = XElement.Parse("<content>" + (string)item.MediaContent + "</content>");
Console.WriteLine((string)mediaContentXml.Element("img").Attribute("src"));    

繰り返しますが、それはきれいではなく、ハックですが、エンコードされた要素のコンテンツが有効な Xml であれば機能します。これを行うより正しい方法は、XmlReader で ConformanceLevel を Fragment に設定し、すべての種類のノードを適切に認識して、対応する Linq to Xml ノードを作成することです。

于 2012-04-25T20:23:47.440 に答える