Remove Xnamespace in Xelement c#

Asked

Viewed 157 times

1

I am in need of removing the Xnamespace that enters as default on an rss return. Follow the code below:

 static void Main(string[] args)
    {

        XNamespace ns = "http://search.yahoo.com/mrss";

         var item = new XElement(ns + "content",
         new XAttribute(XNamespace.Xmlns + "media", ns),
         new XAttribute("duration", "512"),
         new XAttribute("type", "video/mp4"),
         new XAttribute("url", "http://tv-download.dw.de/dwtv_video/flv/gle/gle20140609_ghana_sd_sor.mp4"),
         new XElement(ns + "thumbnail", new XAttribute(XNamespace.Xmlns + "media", ns),
         new XAttribute("url", "http://www.dw.de/image/0,,17611287_403,00.jpg"),
         new XAttribute("type", "image/jpeg"),
         new XAttribute("height", "480"),
         new XAttribute("width", "853")),
         new XElement(ns + "title",
         new XAttribute(XNamespace.Xmlns + "media", ns), "Video caption"),
         new XElement(ns + "description",
         new XAttribute(XNamespace.Xmlns + "media", ns), "description"),
         new XElement(ns + "copyright",
         new XAttribute(XNamespace.Xmlns + "media", ns), "Francisco Cunha, 2014"));


        Console.WriteLine(item);
        Console.ReadLine();
    }
  • Can you please put in your question which XML is expected and which XML is being generated?

  • I need to remove the xmlns:media="" value by only returning the nos values without it for example the value in <media:copyright>Francisco Cunha, 2014</media:copyright> not so <media:copyright xmlns:media="http://search.yahoo.com/mrss">Francisco Cunha, 2014</media:copyright> I’m mounting an rss and have to remove this xmlns from all the ones I’m riding.

1 answer

1


This is because you are forcing the name of namespace as media:

new XAttribute(XNamespace.Xmlns + "media", ns),

Switch to:

new XAttribute(XNamespace.Xmlns, ns),

This should generate the following root element:

<content xmlns="http://search.yahoo.com/mrss">
...
</content>

The other elements should also have the "media" removed, so that tags are generated without the name of namespace.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.