How to parse an xml string or document object to remove namespaces and xml version info

I have a string representation of an object that I converted using serialization, the output is shown below.

I do not want the following line in the string representation

<?xml version="1.0"?>

Also I do not want the namespaces to show up
the final document should look like the foll
<DetailedData>
      <InputRows>
            <InputRow>
              <BusinessDate>3/19/2010</BusinessDate>
              <Region>E</Region>
            </InputRow>
            <InputRow>
              <BusinessDate>3/20/2010</BusinessDate>
              <Region>W</Region>
            </InputRow>
       </InputRows>
</DetailedData>

<?xml version="1.0"?>
<DetailedData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
	<InputRows>
		<InputRow>
		  <BusinessDate>3/19/2010</BusinessDate>
		  <Region>E</Region>
		</InputRow>
		<InputRow>
		  <BusinessDate>3/20/2010</BusinessDate>
		  <Region>W</Region>
		</InputRow>
 	</InputRows>
</DetailedData>


Serialization code
public static string ConvertObjToXml(DetailedData details)
        {
	   XmlSerializer serializer = new XmlSerializer(typeof(DetailedData));
            MemoryStream memoryStream = new MemoryStream();             
            serializer.Serialize(memoryStream, details);           
            memoryStream.Position = 0;
            StreamReader streamreader = new StreamReader(memoryStream);
            string strxml = streamreader.ReadToEnd();
            streamreader.Close();
            memoryStream.Close();
            return strxml;
}

Open in new window

LVL 1
countrymeisterAsked:
Who is Participating?
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.

WesWilsonCommented:
This should do it for you.

XmlDocument xmlDocument;

//load xmlDocument
//...
//

XmlNode encodingNode = null;
if (xmlDocument.FirstChild.Name.StartsWith(XmlResources.literalXml))
{
    encodingNode = xmlDocument.FirstChild;
}
if (encodingNode != null)
{
    xmlDocument.RemoveChild(encodingNode);
}

Open in new window

0
WesWilsonCommented:
Oops. Change Line 8 to:

if (xmlDocument.FirstChild.Name.StartsWith("xml"))

Open in new window

0
käµfm³d 👽Commented:
I cannot test because I don't have a serializable class at hand, but does the following achieve what you are after?
XmlSerializer serializer = new XmlSerializer(typeof(DetailedData));
XmlWriterSettings settings = new XmlWriterSettings();
MemoryStream memoryStream = new MemoryStream();
XmlTextWriter writer = (XmlTextWriter)XmlTextWriter.Create(memoryStream, settings);
XmlDocument xdoc = new XmlDocument();

settings.OmitXmlDeclaration = true;
settings.OutputMethod = XmlOutputMethod.Text;

serializer.Serialize(memoryStream, details);
memoryStream.Position = 0;
xdoc.Load(memoryStream);
xdoc.DocumentElement.Attributes.RemoveAll();
xdoc.Save(memoryStream);
StreamReader streamreader = new StreamReader(memoryStream);
string strxml = streamreader.ReadToEnd();
streamreader.Close();
memoryStream.Close();

return strxml;

Open in new window

0

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
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Mike_MozhaevCommented:
You can avoid namespaces during serialization:

using System.IO;
using System.Xml.Serialization;

public class A
{
      public int X;
}

class App
{
      static void Main()
      {
            A a = new A();

            var ser = new XmlSerializer(typeof(A));
            var ns = new XmlSerializerNamespaces();
            ns.Add("", "");

            using (var f = File.OpenWrite("1.xml"))
            {
                  ser.Serialize(f, a, ns);
            }
      }
}
0
countrymeisterAuthor Commented:
Kaufmed,
The code does not compile - couple of errors

Error      1      Property or indexer 'System.Xml.XmlWriterSettings.OutputMethod' cannot be assigned to -- it is read only

System.InvalidCastException: Unable to cast object of type 'System.Xml.XmlWellFormedWriter' to type 'System.Xml.XmlTextWriter'.
      

0
countrymeisterAuthor Commented:
WesWilson:
If I use your example the remove node logic removes the whole document content
0
countrymeisterAuthor Commented:
I got some ideas from each reply, the code provided was not fully functional
0
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
.NET Programming

From novice to tech pro — start learning today.