Question

I 'm having problem with the code presented:

string serializedLicence = SerializationHelper.ToXML(licenceInfo);
var licenceFileXml = new XElement("Licence", new XElement("LicenceData", serializedLicence)));
XmlDocument signedLicence = SignXml(licenceFileXml.ToString(), Properties.Resources.PRIVATE_KEY);
signedLicence.Save(saveFileDialogXmlLicence.FileName);

The created file has an incorrect coding of strings send to XElement constructors aswell as the signature, that assigned with custom SignXml() method (which creates signature with XmlDocument.DocumentElement.AppendChild() method, but that's irrelevant right now). The output:

<?xml version="1.0" encoding="utf-16" standalone="yes"?>
<Licence>
    <LicenceData>&lt;?xml version="1.0" encoding="utf-16"?&gt;
    &lt;LicenceInfo
    //stuff stuff stuff
    &lt;/LicenceInfo&gt;</LicenceData>
    <Signature>&lt;SignedInfo xmlns="h stuff stuff stuff</Signature>
</Licence>

So basically I'm taking serialized object string and put it between markers, and this part gets encoded wrong. Debugger shows me, that the text in XElement object is holding &lt; and &gt; just after creating it. I could parse it manually, but that's inapropriate.

Note: befeore that, I was straight signing the deserialisation xml and it worked fine, so I can't figure it out why XDocument uses different encoding than XmlSerializer/XmlDocument object.

Also: I think I could just use XmlDocument object to build the file, but I'm curious what's wrong.

Was it helpful?

Solution

You're adding serializedLicence as string, so it's treated as text, not as XML and that's why it looks like that in you document.

var licenceFileXml = new XElement("Licence",
                         new XElement("LicenceData",
                             XDocument.Parse(serializedLicence).Root)));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top