Copying XML Namespace Declaration with Prefixes element from one document to a newly created one using XmlReader.

I have been working on a Windows Form in .NET C# that requires me to search and pull nodes from one XML document and place them into a newly created one using XmlReader. I have everything completed but am having issues copying the whole Namespace Declaration element that contains prefixes. I have tried many different techniques but everything I've tried fails for just as many reasons, mainly I get XML exceptions when trying to append. Below is an example of the format of the element that I'm trying to copy (using a XmlReader) from an existing document.

<prefix:Name xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" prefix:schemaLocation="URL URL" xmlns:Name="URL" xmlns:Name="URL" xmlns:Name="URL" xmlns:Name="URL">

I need copy this into the root element location immediately after the Xml Declaration which I have already created an appended to the document. Also it would be a good idea to mention that I have not been using System.Xml.Linq  and have only been using System.Xml and am trying to stick with that specific .Net Namespace
Russell GarrettAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Russell GarrettAuthor Commented:
solved the issue with XmlSerializer which has the option to add a XmlRootAttribute in the constructor. I just needed to create the XmlRootAttribute and assign it a Namespace value before referencing it in the XmlSerializer constructor, and using a XmlWriter append it to the XmlDocument using a XmlNavigator. Pretty simple once everything was said and done. Below is the code I used.

var nav = xmlDocForExport.CreateNavigator();
XmlReader reader = null;
                    try
                    {
                        reader = XmlReader.Create("SelectedXMLFile");
                        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                        while (reader.Read())
                        {
                            // Grabs the Root element first thing
                            if (reader.IsStartElement())
                            {
                                                // Variables to store the root node information
                                                string RootlocalName = reader.LocalName;
                                string rootPrefix = reader.Prefix;
                                string rootValue = "";
                                                
                                                int counter = reader.AttributeCount;
                                for (int attInd = 0; attInd < counter; attInd++)
                                {
                                                      reader.MoveToAttribute(attInd);
                                                      
                                                      // Variables to store attributes in the root node
                                    string localName = reader.LocalName;
                                    string prefix = reader.Prefix;
                                    string value = reader.Value;
                                                      
                                                      // Checks if the current attribute is the one used as the root node prefix if so assigns the namespace to the rootValue variable
                                                      if (localName == rootPrefix)
                                    {
                                        rootValue = reader.Value;
                                    }
                                                      
                                                      // Assigns the namespace information to the XmlSerializerNamespaces object
                                                      ns.Add(localName, value);
                                                      
                                                      // If the final attribute is processed create the root node with XmlSerializer and append it to the document with the XmlNavigator
                                                      if (attInd == counter - 1)
                                    {
                                                            // Creating the XmlRootAttribute node and giving it a namespace value using the variables that captured the root node information
                                        XmlRootAttribute rootNode = new XmlRootAttribute(RootlocalName);
                                        rootNode.Namespace = rootValue;
                                                            
                                                            // Putting the root node together using XmlSerializer
                                        XmlSerializer xser = new XmlSerializer(typeof(XmlSerializerClass), rootNode);
                                                            
                                                            // Creating an XmlWriter to append the finished XmlRootNode
                                        XmlWriter xmlWriter;
                                        using (xmlWriter = nav.AppendChild())
                                        {
                                            xser.Serialize(xmlWriter, new XmlSerializerClass(), ns);
                                            xmlWriter.Close();
                                        }
                                    }
                                                }
                                                break; // Breaks out of the while loop when everything is done.
                                          }
                                          catch (Exception ex)
                                          {
                                                // Handle Exception
                                          }

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.