需要有关删除xml行的建议

时间:2012-08-22 14:04:02

标签: c# xpath

我正在使用CMS并找到了一个功能,可以从文件夹中的内容生成RSS Feed。但是我希望从列表中删除其中一行。我已经完成了我的研究,我想'我应该使用XmlDocument类来帮助我删除我不想要的行。我已经使用Firebug和FirePath来获取XPath - 但我似乎无法弄清楚如何正确应用它。我也不确定我是否应该使用.Load或.LoadXml - 我使用了后者的seing,好像Feed显示正常。但是我不得不转换ToString()来摆脱那个重载的匹配错误......

我要移除的行称为“存档平面” 我为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>

3 个答案:

答案 0 :(得分: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");
}

答案 1 :(得分:0)

我想我会发布答案,虽然我会将Pauls标记为答案,因为他的代码/建议是我和我进一步研究的基础。仍然不知道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); }
}

答案 2 :(得分: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

请参阅this msdn link for more info