Connecting Tech Pros Worldwide Forums | Help | Site Map

XmlDocument.save bug? Appending [] to the doctype

Stephen Ward
Guest
 
Posts: n/a
#1: Aug 7 '08

I have a simple little project open a xml file change a few nodes save the
file, no big deal.

The problem is that the doctype is getting modified when I save the file.

So it looks like this: <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
But when I save it : <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd"[]>

You can see at the end it appends the [] which invalidates the file. No
where in the code do I specifically modify the doctype. Is there a way to
prevent the doctype from getting modified , this appears to be a bug. As a
workaround Išve replaced the Doctype node. But this is a kluge.



Snippet
XmlNodeList myList;
XmlDocument myDoc = new XmlDocument();

myDoc.Load("c:\\inetpub\\wwwroot\\template.mobilec onfig");

myList= myDoc.GetElementsByTagName("key");

do some stuff .....


XmlTextWriter w = new XmlTextWriter("C:\\xmlFiles\\" + username +
".mobileconfig",new UTF8Encoding(false));

myDoc.Save(w);


Jeroen Mostert
Guest
 
Posts: n/a
#2: Aug 7 '08

re: XmlDocument.save bug? Appending [] to the doctype


Stephen Ward wrote:
Quote:
>
I have a simple little project open a xml file change a few nodes save
the file, no big deal.
>
The problem is that the doctype is getting modified when I save the file.
>
So it looks like this: <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST
1.0//EN" "_http://www.apple.com/DTDs/PropertyList-1.0.dtd_">
But when I save it : <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST
1.0//EN" "_http://www.apple.com/DTDs/PropertyList-1.0.dtd_"[]>
>
You can see at the end it appends the [] which invalidates the file.
It does not invalidate the file, actually, in the sense that it's still
legal XML. The doctype can contain internal declarations; this simply
specifies an empty declaration list. In this sense, the (abstract) doctype
is not actually modified, it's just not literally what was read in.

I agree that this is not desirable behavior, though. Ideally round-tripping
should introduce as few changes as possible.
Quote:
No where in the code do I specifically modify the doctype. Is there a
way to prevent the doctype from getting modified , this appears to be a
bug. As a workaround I’ve replaced the Doctype node. But this is a kluge.
>
I've seen worse. You can do it fairly transparently with

xmlDocument.ReplaceChild(
xmlDocument.CreateDocumentType(
xmlDocument.DocumentType.Name,
xmlDocument.DocumentType.PublicId,
xmlDocument.DocumentType.SystemId,
string.IsNullOrEmpty(xmlDocument.DocumentType.Inte rnalSubset) ? null
: xmlDocument.DocumentType.InternalSubset
),
xmlDocument.DocumentType
);

The problem is that "InternalSubset" is an empty string after reading it in,
when it should be null to omit the subset declaration altogether.

--
J.
Stephen Ward
Guest
 
Posts: n/a
#3: Aug 8 '08

re: XmlDocument.save bug? Appending [] to the doctype


Your correct its a legal xml file. BUT the device that uses the file plus
the apple config software that reads the file barf when it sees the [ ] .

So I need to have it print out literally what was read in. Thanks if anyone
can figure this out.


On 8/7/08 12:25 PM, in article 489b4c27$0$49822$e4fe514c@news.xs4all.nl,
"Jeroen Mostert" <jmostert@xs4all.nlwrote:
Quote:
Stephen Ward wrote:
Quote:
>>
>I have a simple little project open a xml file change a few nodes save
>the file, no big deal.
>>
>The problem is that the doctype is getting modified when I save the file.
>>
>So it looks like this: <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST
>1.0//EN" "_http://www.apple.com/DTDs/PropertyList-1.0.dtd_">
>But when I save it : <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST
>1.0//EN" "_http://www.apple.com/DTDs/PropertyList-1.0.dtd_"[]>
>>
>You can see at the end it appends the [] which invalidates the file.
>
It does not invalidate the file, actually, in the sense that it's still
legal XML. The doctype can contain internal declarations; this simply
specifies an empty declaration list. In this sense, the (abstract) doctype
is not actually modified, it's just not literally what was read in.
>
I agree that this is not desirable behavior, though. Ideally round-tripping
should introduce as few changes as possible.
>
Quote:
>No where in the code do I specifically modify the doctype. Is there a
>way to prevent the doctype from getting modified , this appears to be a
>bug. As a workaround Išve replaced the Doctype node. But this is a kluge.
>>
I've seen worse. You can do it fairly transparently with
>
xmlDocument.ReplaceChild(
xmlDocument.CreateDocumentType(
xmlDocument.DocumentType.Name,
xmlDocument.DocumentType.PublicId,
xmlDocument.DocumentType.SystemId,
string.IsNullOrEmpty(xmlDocument.DocumentType.Inte rnalSubset) ? null
: xmlDocument.DocumentType.InternalSubset
),
xmlDocument.DocumentType
);
>
The problem is that "InternalSubset" is an empty string after reading it in,
when it should be null to omit the subset declaration altogether.
Jeroen Mostert
Guest
 
Posts: n/a
#4: Aug 8 '08

re: XmlDocument.save bug? Appending [] to the doctype


Stephen Ward wrote:
Quote:
Your correct its a legal xml file. BUT the device that uses the file plus
the apple config software that reads the file barf when it sees the [ ] .
>
Yes, I'd expect there to be quite a few parsers that don't like it.
Quote:
So I need to have it print out literally what was read in. Thanks if anyone
can figure this out.
>
I'm fairly sure there are no easy settings or switches you can toggle that
will make this disappear (I've looked at the implementation). Using
XPathNavigator or XDocument won't avail you either; these classes use
XmlTextReader under the covers. So does everything else in .NET that needs
to parse XML somehow, I'd wager. Short of reimplementing XmlTextReader
(which for obvious reasons I don't recommend) there's no clean solution.

If you're concerned about it, file a bug with Connect, but otherwise, a
workaround is going to be necessary for the foreseeable future. Doctypes in
XML are not common to begin with, so I wouldn't hold my breath.

--
J.
Closed Thread