简体   繁体   中英

how to access xml element with prefix or namespace in c#

hey can any one help me ... i am also facing the same problems but the solution given is not working for me . i also googled so much ... but unable to find any solution ...

Effort that i made , finally look like ---

protected void GetRSS(string url)
{
    WebRequest MyRssRequest = WebRequest.Create(url);
    WebResponse MyRssResponse = MyRssRequest.GetResponse();
    Stream MyRssStream = MyRssResponse.GetResponseStream();
    XmlDocument MyRssDocument = new XmlDocument();
    MyRssDocument.Load(MyRssStream);

    XmlNamespaceManager nsmgr = new XmlNamespaceManager(MyRssDocument.NameTable);
    nsmgr.AddNamespace("dc", "http://purl.org/dc/elements/1.1/");

    XmlNamespaceManager nsmgrcontent = new XmlNamespaceManager(MyRssDocument.NameTable);
    nsmgr.AddNamespace("content", "http://purl.org/rss/1.0/modules/content/");

    XmlNamespaceManager nsmgrwfw = new XmlNamespaceManager(MyRssDocument.NameTable);
    nsmgr.AddNamespace("wfw", "http://wellformedweb.org/CommentAPI/");

    XmlNamespaceManager nsmgratom = new XmlNamespaceManager(MyRssDocument.NameTable);
    nsmgr.AddNamespace("atom", "http://www.w3.org/2005/Atom");

    XmlNamespaceManager nsmgrsy = new XmlNamespaceManager(MyRssDocument.NameTable);
    nsmgr.AddNamespace("sy", "http://purl.org/rss/1.0/modules/syndication/");

    XmlNamespaceManager nsmgrslash = new XmlNamespaceManager(MyRssDocument.NameTable);
    nsmgr.AddNamespace("slash", "http://purl.org/rss/1.0/modules/slash/");

    XmlNamespaceManager nsmgrgeorss = new XmlNamespaceManager(MyRssDocument.NameTable);
    nsmgr.AddNamespace("georss", "http://www.georss.org/georss");

    XmlNamespaceManager nsmgrgeo = new XmlNamespaceManager(MyRssDocument.NameTable);
    nsmgr.AddNamespace("geo", "http://www.w3.org/2003/01/geo/wgs84_pos#");

    XmlNamespaceManager nsmgrmedia = new XmlNamespaceManager(MyRssDocument.NameTable);
    nsmgr.AddNamespace("media", "http://search.yahoo.com/mrss/");


    XmlNodeList MyRssList = MyRssDocument.SelectNodes("rss/channel/item");
    string sTitle = "";
    string sLink = "";
    string sDescription = "";
    string sPubDate = "";
    string sCreator = "";
    // Iterate/Loop through RSS Feed items
    for (int i = 0; i < MyRssList.Count; i++)
    {
        XmlNode MyRssDetail;

        MyRssDetail = MyRssList.Item(i).SelectSingleNode("title");
        if (MyRssDetail != null)
        {
            sTitle = MyRssDetail.InnerText;
        }
        else
        {
            sTitle = "";
        }

        MyRssDetail = MyRssList.Item(i).SelectSingleNode("link");
        if (MyRssDetail != null)
        {
            sLink = MyRssDetail.InnerText;
        }
        else
        {
            sLink = "";
        }

        MyRssDetail = MyRssList.Item(i).SelectSingleNode("description");
        if (MyRssDetail != null)
        {
            sDescription = MyRssDetail.InnerText;
        }
        else
        {
            sDescription = "";
        }

        MyRssDetail = MyRssList.Item(i).SelectSingleNode("pubDate");
        if (MyRssDetail != null)
        {
            sPubDate = MyRssDetail.InnerText;
        }
        else
        {
            sPubDate = "";
        }

        MyRssDetail = MyRssList.Item(i).SelectSingleNode("creator", nsmgr);
        if (MyRssDetail != null)
        {
            sCreator = MyRssDetail.InnerText;
        }
        else
        {
            sCreator = "";
        }

        string JournalistName = MyRssList.Item(i).SelectSingleNode("lastBuildDate").NextSibling.InnerText;

        HtmlTableCell block = new HtmlTableCell();
        // You can style the Title From Here
        block.InnerHtml = "<span style='font-weight:bold'><a href='" + sLink + "' target='new'>" + sTitle + "</a></span>";               
        HtmlTableRow row = new HtmlTableRow();
        row.Cells.Add(block);
        tbl_Feed_Reader.Rows.Add(row);

        HtmlTableCell block_description = new HtmlTableCell();
        //You can style the Description from here
        block_description.InnerHtml = "<p align='justify'>" + sDescription + "</p>";
        HtmlTableRow row2 = new HtmlTableRow();
        row2.Cells.Add(block_description);
        tbl_Feed_Reader.Rows.Add(row2);

        HtmlTableCell block_pubdate = new HtmlTableCell();
        //You can style the Description from here
        block_pubdate.InnerHtml = "<p align='justify'>" + sPubDate + "</p>";
        HtmlTableRow row3 = new HtmlTableRow();
        row3.Cells.Add(block_pubdate);
        tbl_Feed_Reader.Rows.Add(row3);


        HtmlTableCell block_creator = new HtmlTableCell();
        //You can style the Description from here
        block_creator.InnerHtml = "<p align='justify'><b>" + JournalistName + "</b></p>";
        HtmlTableRow row4 = new HtmlTableRow();
        row4.Cells.Add(block_creator);
        tbl_Feed_Reader.Rows.Add(row4);

    }
}

and the section given below is giving me null as element ...

MyRssDetail = MyRssList.Item(i).SelectSingleNode("creator", nsmgr);
if (MyRssDetail != null)
{
    sCreator = MyRssDetail.InnerText;
}
else
{
    sCreator = "";
}

If "creator" is within a segment in the XML file that has a namespace.

<widget xmlns="http://www.w3.org/2001/XMLSchema">
  <creator>Widget Creator</creator>
</widget>

Then

nsmgr.AddNamespace("blah", "http://www.w3.org/2001/XMLSchema");

And this becomes...

MyRssList.Item(i).SelectSingleNode("blah:creator", nsmgr);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM