none
How can I save an XML structure without declaration? RRS feed

  • Question

  • Hello,
    I'm using namespace empty, nevertheless the header is written.
    Can I get a string via the xmlWriter without re-reading it?
    Thanks for tips in advance.
    protected void SaveRequestXML<T>(string filePath, T dataObject)
    {
    	XmlSerializer serializer = new XmlSerializer(typeof(T));
    
    	using (Stream stream = new FileStream(filePath, FileMode.Create))
    	{
    		using (XmlWriter xmlWriter = XmlWriter.Create(stream))
    		{
    			serializer.Serialize(xmlWriter, dataObject);
    		}
    	}
    	StreamReader reader = new StreamReader(filePath);
    	string contents = reader.ReadToEnd();
    	Log.Info("Request Message\n \n" + contents);
    }
    
    [XmlType(AnonymousType = true)]
    [XmlRoot(Namespace = "", IsNullable = false)]
    public class Request
    {
    	/// <remarks/>
    	public uint OrderNum { set; get; }
    }
    With best regards Markus
    Tuesday, December 17, 2019 6:01 PM

All replies

  • Hi Markus,

    Thank you for posting here.

    I'm not sure what this sentence means: Can I get a string via the xmlWriter without re-reading it?

    If you want to delete the namespace, you can do it like this:

      xmlDocument.DocumentElement.RemoveAllAttributes();

    If you want to set an empty namespace, do it like this:

     xmlDocument.DocumentElement.SetAttribute("xmlns:xsi", "");

    Hope this could be helpful.

    Best Regards, 

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 18, 2019 5:22 AM
  • Hello Timon,

    My question was, is there a way to easily solve with serialization?

    Can I get a string via XMLWriter without using StreamReader?

    Maybe not possible. Can you please answer yes or no? Or someone who knows.

       Thanks in advance.

    Best regards Markus

    Tuesday, December 24, 2019 4:08 PM
  • Hi Markus,

    So far, I have not found a feasible solution.

    If there are any new ideas, I will inform you immediately.

    Sorry for that.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 25, 2019 1:48 AM