How do I add a custom XmlDeclaration with XmlDocument/XmlDeclaration?

I would like to create a custom XmlDeclaration while using the XmlDocument/XmlDeclaration classes in c# .net 2 or 3.

This is my desired output (it is an expected output by a 3rd party app):

<?xml version="1.0" encoding="ISO-8859-1" ?>
<?MyCustomNameHere attribute1="val1" attribute2="val2" ?>
[ ...more xml... ]

Using the XmlDocument/XmlDeclaration classes, it appears I can only create a single XmlDeclaration with a defined set of parameters:

XmlDocument doc = new XmlDocument();
XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "ISO-8859-1", null);
doc.AppendChild(declaration);

Is there a class other than the XmlDocument/XmlDeclaration I should be looking at to create the custom XmlDeclaration? Or is there a way with the XmlDocument/XmlDeclaration classes itself?

Answers


What you are wanting to create is not an XML declaration, but a "processing instruction". You should use the XmlProcessingInstruction class, not the XmlDeclaration class, e.g.:

XmlDocument doc = new XmlDocument();
XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "ISO-8859-1", null);
doc.AppendChild(declaration);
XmlProcessingInstruction pi = doc.CreateProcessingInstruction("MyCustomNameHere", "attribute1=\"val1\" attribute2=\"val2\"");
doc.AppendChild(pi);

Need Your Help

Automatically check for iPad app when downloading specific file type from website?

ios ipad

I think I already know the answer to this ("can't be done"), but I figured I'd see what people think...

Java String pattern that takes only specific strings

java regex string matching

Following is the bulk data I have to go through:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.