Andrew Roth
Andrew Roth

Reputation: 1073

How to place XML Processing Instruction on Line 1 using System.XML.Linq

I am writing a console application that generates an XML file that will be consumed by a server job processing application that was written a long time ago. The server app requires a processing instruction: <?JtJob jobname?>. I'm using C# XDocument to generate my xml:

XDocument xml = new XDocument(new XProcessingInstruction("JtJob", "FieldInspection3_Rejected"),
    new XElement("Document",
        new XElement("DataFile", tempFileName),
        new XElement("FormType","Corrected Form Package"),
        new XElement("BYOD_RejectComment",reasonForRejection),
        new XElement("BYOD_FromTech",techEmail)
    )
);
xml.Save(Path.Combine("C:\\Data", DateTime.Now.ToString("yyyyMMdd_HHmmssffff") + "_Rejected.xml"));

For some reason, the server app requires the processing instruction to be on the first line. If my xml file looks like this:

<?xml version="1.0" encoding="utf-8"?><?JtJob FieldInspection3_Rejected?>
<Document>
  <DataFile>C:\Windows\TEMP\tmp387F.tmp</DataFile>
  <FormType>Corrected Form Package</FormType>
  <BYOD_RejectComment>you're ugly</BYOD_RejectComment>
  <BYOD_FromTech>[email protected]</BYOD_FromTech>
</Document>

Everything works fine. But when it looks like this:

<?xml version="1.0" encoding="utf-8"?>
<?JtJob FieldInspection3_Rejected?>
<Document>
  <DataFile>C:\Windows\TEMP\tmp387F.tmp</DataFile>
  <FormType>Corrected Form Package</FormType>
  <BYOD_RejectComment>you're ugly</BYOD_RejectComment>
  <BYOD_FromTech>[email protected]</BYOD_FromTech>
</Document>

It errors. My problem is, using the XDocument code above, it generates the second output.

Without loading my generated xml back in as a string and manipulating the string, is there a way for me to tell XDocument to create the processing instruction on the first line?

I know the blame is definitely to be placed on the server app for not accepting valid XML syntax, but my goal is to get this to work, not fix a 20 year old program.

Edit: Thanks! Using the save override preserved the formatting. Didn't make it all one line, but it allowed me to keep the PI on line 1.

Edit 2: Well, that didn't help me either. But I found out what would help me! XDocument.Save() by default outputs UTF8 With BOM. I changed it to without BOM by using XMLTextWriter and that worked.

Upvotes: 0

Views: 499

Answers (2)

User
User

Reputation: 1118

You'll want to use a XDocument.Save() overload that allows you to specify formatting options:

xml.Save(Path.Combine("C:\\Data", DateTime.Now.ToString("yyyyMMdd_HHmmssffff") + "_Rejected.xml"),
         SaveOptions.DisableFormatting);

https://msdn.microsoft.com/en-us/library/bb551426(v=vs.110).aspx

Upvotes: 0

test
test

Reputation: 2639

What if you used the XDocument.Save(String, SaveOptions) method to get an output all on a single line?

So do this instead:

xml.Save(fileName, SaveOptions.DisableFormatting);

This would force the declaration to be onto the first line with the downside of having the entire document on the first line, but if it works for that program then so be it.

Upvotes: 1

Related Questions