Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6072691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:11:17+00:00 2026-05-23T10:11:17+00:00

I have an input RSS Feed with some elements already added with namespace prefix

  • 0

I have an input RSS Feed with some elements already added with namespace prefix (for itunes). Without removing attribute and adding in C# again, an element, say <itunes:subtitle> is added as namespace and the element is <subtitle>

Desired output:

 <rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd/" version="2.0">
      <channel>
        <title>channelTitle</title>
        <itunes:subtitle>subtitle_description</itunes:subtitle>
        <item>
          <title>item1</title>
          <itunes:subtitle>A short description</itunes:subtitle>
        </item>
      </channel>
    </rss>

Input XML:

 <rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd/" version="2.0">
      <channel>

        <item>
          <title>item1</title>
          <itunes:subtitle>A short description</itunes:subtitle>
        </item>
      </channel>
    </rss>

How can I add another element in C#, but also maintain the existing namespace:element ? I’m having to explicitly add the namespace again in the code (and namespace should also be present in input XML, otherwise it’s processing invalid XML:

See code:

 XNamespace itunes = "http://www.itunes.com/dtds/podcast-1.0.dtd/";
     string rssFeed = "<rss xmlns:itunes=\"http://www.itunes.com/dtds/podcast-1.0.dtd\" version=\"2.0\"><channel><item><title>item1</title><itunes:subtitle>A short description</itunes:subtitle></item></channel></rss>";

     XDocument XMLDoc = XDocument.Parse(rssFeed);

     XMLDoc.Root.RemoveAttributes();     
     XMLDoc.Root.Add(new XAttribute(XNamespace.Xmlns + "itunes", itunes.NamespaceName));

    //Without adding the namespace attribute explicitly, the xmlns attribute is added instead to <subtitle> instead of <itunes:subtitle> : 
      XMLDoc.Element("rss").Element("channel").AddFirst(
                    new XElement("title", "channelTitle"),
                    new XElement(itunes + "subtitle", "subtitle_description")
                );

gets added correctly.
However, is now changed in the input XML, output:

<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd/">
  <channel>
    <title>channelTitle</title>
    <itunes:subtitle>subtitle_description</itunes:subtitle>
    <item>
      <title>item1</title>
      <subtitle xmlns="http://www.itunes.com/dtds/podcast-1.0.dtd">A short description</subtitle>
    </item>
  </channel>
</rss>

Approach2:

  XNamespace itunes = "http://www.itunes.com/dtds/podcast-1.0.dtd/";
  string rssFeed = "<rss xmlns:itunes=\"http://www.itunes.com/dtds/podcast-1.0.dtd\" version=\"2.0\"><channel><item><title>item1</title><itunes:subtitle>A short description</itunes:subtitle></item></channel></rss>";

           XDocument XMLDoc = XDocument.Parse(rssFeed);

            XMLDoc.Element("rss").Element("channel").AddFirst(
                new XElement("title", "channelTitle"),
                new XElement(itunes + "subtitle", "subtitle_description")
            );

            Console.WriteLine(XMLDoc);

Output:

<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">
  <channel>
    <title>channelTitle</title>
    <subtitle xmlns="http://www.itunes.com/dtds/podcast-1.0.dtd/">subtitle_description</subtitle>
    <item>
      <title>item1</title>
      <itunes:subtitle>A short description</itunes:subtitle>
    </item>
  </channel>
</rss>

As much as my question is really long, I’m hoping to get few lines of code as an answer, sure there must be something simple I’m missing 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:11:18+00:00Added an answer on May 23, 2026 at 10:11 am

    I can’t reproduce the problem you say you face, the following straightforward sample

        string rssFeed = "<rss xmlns:itunes=\"http://www.itunes.com/dtds/podcast-1.0.dtd\" version=\"2.0\"><channel><item><title>item1</title><itunes:subtitle>A short description</itunes:subtitle></item></channel></rss>";
    
        XDocument XMLDoc = XDocument.Parse(rssFeed);
    
        XNamespace itunes = XMLDoc.Root.GetNamespaceOfPrefix("itunes");
    
        XMLDoc.Element("rss").Element("channel").AddFirst(
                      new XElement("title", "channelTitle"),
                      new XElement(itunes + "subtitle", "subtitle_description")
                  );
    
        XMLDoc.Save(Console.Out);
        Console.WriteLine();
    

    when run with .NET 3.5, outputs

    <rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">
      <channel>
        <title>channelTitle</title>
        <itunes:subtitle>subtitle_description</itunes:subtitle>
        <item>
          <title>item1</title>
          <itunes:subtitle>A short description</itunes:subtitle>
        </item>
      </channel>
    </rss>
    

    Looking closer at your code, the only problem is see is that some samples use the URI http://www.itunes.com/dtds/podcast-1.0.dtd while some have http://www.itunes.com/dtds/podcast-1.0.dtd/ with a trailing slash “/”. So that might be the reason why you run into problems.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have some input data that sometimes appears with &nbsp characters on the end.
I have an input which at some points happens to have the focus. If
I have several input and option elements on my page, each (well almost) have
I have created a php script to import rss feed into the database. The
I have input tags with a user defined attribute as:- <input name=grp1 type=radio myUDF=value1
I have input fields, which I process with AJAX and send it on another
Currently I have Input: e.g., '123456789'.'+'.'987654321' Desired Pattern: Output: e.g., '123456789987654321' How can I
I have an arraylist set up. I have input instuctions set up too, so
How to rewrite Webconfig connection string at runtime.I have input textbox for Server,UserName and
I have saved input from a textarea element to a TEXT column in MySQL.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.