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?
 
käµfm³d 👽Connect With a Mentor 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
 
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
 
WesWilsonConnect With a Mentor Commented:
Oops. Change Line 8 to:

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

Open in new window

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Mike_MozhaevConnect With a Mentor Commented:
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
All Courses

From novice to tech pro — start learning today.