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

  • SEARCH
  • Home
  • 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 8844679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:32:52+00:00 2026-06-14T11:32:52+00:00

I need to create an XmlDocument partly by using old XML and partly by

  • 0

I need to create an XmlDocument partly by using old XML and partly by creating new. The problem is that the old XML contains custom namespaces and I can’t seem to be able to use them as I get an XmlException. I’ve tried to add the namespace to many different places but I can’t get over the Exception!

The Exception

System.Xml.XmlException was unhandled by user code
    Message='my' is an undeclared prefix. Line 1, position 42.
    Source=System.Xml

My Code

XmlDocument doc = new XmlDocument();
XmlSchema schema = new XmlSchema();
schema.Namespaces.Add("my", "http://foobar.com/");
doc.Schemas.Add(schema);
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(dec);

XmlElement root = doc.CreateElement("root");
root.SetAttribute("xmlns:my", "http://foobar.com/");
doc.AppendChild(root);

foreach (var item in GetItems())
{
    XmlElement elem = doc.CreateElement("item");
    elem.SetAttribute("id", item.id);

    // Append body to elem
    XmlElement body = doc.CreateElement("body");
    body.InnerXml = item.Body; // Here is where I get the exception

    elem.AppendChild(body);

    // Append elem to root
    root.AppendChild(elem);
}

Input from Item.Body is similar to

<aaa><bbb my:attr="55">Foo</bbb></aaa>

I expected the output to be similar to

<?xml version="1.0" encoding="utf-8"?>
<root my:attr="http://foobar.com/">
  <item id="12345">
    <body>
        <aaa>
            <bbb my:attr="55">Foo</bbb>
        </aaa>
    </body>
  </item>
</root>

I’m open to alternatives to using this method. After I create the XmlDocument I prettyprint it, validate it against a schema and then push it out for the user to see.

  • 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-06-14T11:32:54+00:00Added an answer on June 14, 2026 at 11:32 am

    The following is a workaround, best I can come up with:

     XNamespace  my = "http://foobar.com/";
    
     var doc = new XDocument(new XElement("root", 
                    new XAttribute(XNamespace.Xmlns +  "my", my)));
    
     var body = new XElement("body");
     doc.Root.Add(new XElement("item", new XAttribute("id", 12345), body));
    
     string innerItem = @"<aaa><bbb my:attr=""55"">Foo</bbb></aaa>";       
     string itemWrap = @"<wrap xmlns:my=""http://foobar.com/"">" + innerItem + "</wrap>";
    
     XElement item = XElement.Parse(itemWrap);
     body.Add(item.Element("aaa"));
    
     Console.WriteLine(doc);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a XML Document object using the NodeList. Can someone pls
I need to manipulate an existing XML document, and create a new one from
Need to create a custom DNS name server using C which will check against
I need to create a unit test for a method that returns an xmldocument.
I need to validate a string that contains XML Data, there is no schema
I need to serialize an object using datacontract serializer. The problem is that i
I need to create an XML schema that validates a tree structure of an
I need to create an XmlDocument with a root element containing multiple namespaces. Am
I need create custom dialog and put JPanel into it. Is it possible?
I need create clone repository. but I do not know where can I get

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.