0

私は CMS を使用しており、フォルダー内のコンテンツから RSS フィードを生成する機能を見つけました。ただし、リストから行の1つを削除したいと思います。私は調査を行いましたが、不要な行を削除するために XmlDocument クラスを使用する必要があると「考えています」。Firebug と FirePath を使用して XPath を取得しましたが、適切に適用する方法がわかりません。.Load と .LoadXml のどちらを使用する必要があるかもわかりません。フィードが正常に表示されるかのように、後者を使用しました。ただし、そのオーバーロードされた一致エラーを取り除くために ToString() を変換する必要がありました....

削除したい行は「Archived Planes」と呼ばれ、FirePath で取得した XPath は「.//*[@id='feedContent']/xhtml:div[11]/xhtml:h3/xhtml:a」です。

また、 .RemoveChild(node); と仮定しています。Response.Write の前に rssData から削除します。ありがとう

Object rssData = new object();
Cms.UI.CommonUI.ApplicationAPI AppAPI = new Cms.UI.CommonUI.ApplicationAPI();
rssData = AppAPI.ecmRssSummary(50, true, "DateCreated", 0, "");
Response.ContentType = "text/xml";

XmlDocument xmlDocument = new XmlDocument();

xmlDocument.LoadXml(rssData.ToString());

XmlNode node = xmlDocument.SelectSingleNode(@"xhtml:div/xhtml:h3/xhtml[a = 'Archived Planes']");

    if (node != null)
    {
        node.ParentNode.RemoveChild(node);
    }

Response.Write(rssData);

以下の出力を含むように編集

This is the what the response.write from rssData is pumping out:
<?xml version="1.0" ?> 
<rss xmlns:xsd="http://www.w3.org/2001/XMLSchema"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0">
<channel>
<title>Plane feed</title>
<link>http://www.domain.rss1.aspx</link>
<description></description>
<item>
<title>New Planes</title>
<link>http://www.domainx1.aspx</link>
<description>
This is the description
</description>
<author>Andrew</author>
<pubDate>Thu, 16 Aug 2012 15:55:53 GMT</pubDate>
</item>
<item>
<title>Archived Planes</title>
<link>http://www.domain23.aspx</link>
<description>
Description of Archived Planes
</description>
<author>Jan</author>
<pubDate>Wed, 15 Aug 2012 10:34:23 GMT</pubDate>
</item>
</channel>
</rss>

4

3 に答える 3

1

あなたの xpath が間違っていると思われます。参照しているファンキーな dom 要素のように見えますが、xml 要素ではありません...たとえば、次の xml の場合

<?xml version="1.0" encoding="UTF-16" standalone="yes"?>
<NewDataSet>
 <userinfo>
     <username>pqr2</username>
     <pass>abc</pass>
     <addr>abc</addr>
 </userinfo>

 <userinfo>
     <username>pqr1</username>
     <pass>pqr2</pass>
     <addr>pqr3</addr>
 </userinfo>
</NewDataSet>

このコードは、ユーザー名要素が pqr1 の userinfo ノードを削除します。

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"file.xml");
XmlNode node = xmlDocument.SelectSingleNode(@"NewDataSet/userinfo[username = 'pqr1']");

if (node != null) {
  node.ParentNode.RemoveChild(node);
  xmlDocument.Save(@"file.xml");
}
于 2012-08-22T14:59:43.890 に答える
0

彼のコード/アドバイスがこれと私のさらなる研究の基礎だったので、私はポールズを答えとしてマークしますが、私は答えを投稿すると思っていました。SelectSingleNode の '@' が何であるか、本当にそれを使用する必要があるかどうかはまだわかりません。さらに調査を行います。

    Object rssData = new object();
    Cms.UI.CommonUI.ApplicationAPI AppAPI = new Cms.UI.CommonUI.ApplicationAPI();
    rssData = AppAPI.ecmRssSummary(50, true, "DateCreated", 0, "");
    Response.ContentType = "text/xml";
    Response.ContentEncoding = System.Text.Encoding.UTF8;

    XmlDocument xmlDocument = new XmlDocument();

    xmlDocument.LoadXml(rssData.ToString());

    XmlNode node = xmlDocument.SelectSingleNode("rss/channel/item[title = 'Archived Planes']");

    if (node != null)
        try
        {
            node.ParentNode.RemoveChild(node);
            xmlDocument.Save(Response.Output);
        }
        catch { }

    else { Response.Write(rssData); }
}
于 2012-08-22T21:47:58.857 に答える
0

@ 記号は、そのままの文字列リテラルを示すためのものです (通常の文字列宣言と比較して、文字列にファンキーな文字を含めることができます)。

string e = "Joe said \"Hello\" to me";      // Joe said "Hello" to me
string f = @"Joe said ""Hello"" to me";   // Joe said "Hello" to me

詳細については、この msdn リンクを参照してください

于 2012-08-23T09:12:23.207 に答える