I am not accustomed to creating xml files programmatically. The big picture is this: This will be in VB/VS 2005/ winforms. I have a DTD, a sample XML, and an outside data source I will use to populate the XML. In general I think I know how to create elements and attributes and I'm sure I can hack something together but I'm wondering if I'm missing (or forgetting) a better way (other than a long line of hard-coded CreateElement commands). Suggestions please.
-----
A related problem: the XML header, for lack of a better term. If we assume creation of an XmlDocument object from scratch, how do I add the header elements? I cannot find any discussion of this. (Feel free to correct my terminology.)
This is what I'm referring to -
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE SOME TEXT "DTDName.dtd"> <?xml-stylesheet href="SampleXml.xslt" type="text/xsl" ?>
As to the header you shouldn't need to add it. It is created when the document is serialised and isn't always needed, it depends on the encoding used. I agree that creating documents by dozens of CreateXXX statements is not nice. How changeable are the documents. I often start with a template, either a string constant, in a resource file or an actual file and load this first. Then I alter it as needed.
>I am not accustomed to creating xml files programmatically. The big >picture > is this: This will be in VB/VS 2005/ winforms. I have a DTD, a sample > XML, > and an outside data source I will use to populate the XML. In general I > think I know how to create elements and attributes and I'm sure I can hack > something together but I'm wondering if I'm missing (or forgetting) a > better > way (other than a long line of hard-coded CreateElement commands). > Suggestions please.
> -----
> A related problem: the XML header, for lack of a better term. If we > assume > creation of an XmlDocument object from scratch, how do I add the header > elements? I cannot find any discussion of this. (Feel free to correct my > terminology.)
> This is what I'm referring to -
> <?xml version="1.0" encoding="utf-8" ?> > <!DOCTYPE SOME TEXT "DTDName.dtd"> > <?xml-stylesheet href="SampleXml.xslt" type="text/xsl" ?>
B. Chernick wrote: > I am not accustomed to creating xml files programmatically. The big picture > is this: This will be in VB/VS 2005/ winforms. I have a DTD, a sample XML, > and an outside data source I will use to populate the XML. In general I > think I know how to create elements and attributes and I'm sure I can hack > something together but I'm wondering if I'm missing (or forgetting) a better > way (other than a long line of hard-coded CreateElement commands). > Suggestions please.
I don't mean to be rude but I'm afraid I didn't understand much your reply.
This, so far as I know, is not a standard Dot Net situation. The purpose of the program, if it ever gets written, is to extract data from an Excel spreadsheet and plug it into a standalone XML file. I'm afraid I've been given very little of the big picture. I think it eventually gets fed into some mainframe system.
"Joe Fawcett" wrote: > As to the header you shouldn't need to add it. It is created when the > document is serialised and isn't always needed, it depends on the encoding > used. > I agree that creating documents by dozens of CreateXXX statements is not > nice. > How changeable are the documents. I often start with a template, either a > string constant, in a resource file or an actual file and load this first. > Then I alter it as needed.
> "B. Chernick" <BChern...@discussions.microsoft.com> wrote in message > news:D332B170-4DDF-4D4B-9F8E-A4B233E7C652@microsoft.com... > >I am not accustomed to creating xml files programmatically. The big > >picture > > is this: This will be in VB/VS 2005/ winforms. I have a DTD, a sample > > XML, > > and an outside data source I will use to populate the XML. In general I > > think I know how to create elements and attributes and I'm sure I can hack > > something together but I'm wondering if I'm missing (or forgetting) a > > better > > way (other than a long line of hard-coded CreateElement commands). > > Suggestions please.
> > -----
> > A related problem: the XML header, for lack of a better term. If we > > assume > > creation of an XmlDocument object from scratch, how do I add the header > > elements? I cannot find any discussion of this. (Feel free to correct my > > terminology.)
> > This is what I'm referring to -
> > <?xml version="1.0" encoding="utf-8" ?> > > <!DOCTYPE SOME TEXT "DTDName.dtd"> > > <?xml-stylesheet href="SampleXml.xslt" type="text/xsl" ?>
Thanks for your response. I think you may have answered several other questions I've been wondering about. However in this particular situation, I don't believe converting the DTD to an xsd is an option. The output has to go to some legacy system that I don't have control over (so far as I know).
(I would have to translate the DTD manually, right? There's no wizard or utility for such a conversion? If I am not mistaken, DTDs obsolete and there's little or no support for them in Dot Net?)
"Martin Honnen" wrote: > B. Chernick wrote: > > I am not accustomed to creating xml files programmatically. The big picture > > is this: This will be in VB/VS 2005/ winforms. I have a DTD, a sample XML, > > and an outside data source I will use to populate the XML. In general I > > think I know how to create elements and attributes and I'm sure I can hack > > something together but I'm wondering if I'm missing (or forgetting) a better > > way (other than a long line of hard-coded CreateElement commands). > > Suggestions please.
B. Chernick wrote: > (I would have to translate the DTD manually, right? There's no wizard or > utility for such a conversion? If I am not mistaken, DTDs obsolete and > there's little or no support for them in Dot Net?)
> As for .NET, it has support for validating an XML document against a DTD > but besides that its API are pretty much focussed on W3C XML schemas.
Pretty much as I suspected. Getting approval for 3rd party tools is problematic as well, especially for adhoc projects. :-)
On an earlier point, the use of WriteDocType, I am ready to tear my hair out. I have this code and I cannot get it to work. I keep getting the error 'The '' character, hexiadecimal value 0x20, cannot be included in a name.' Frankly I'm out of my league here. I cannot find any reference for the error or WriteDocType that I can understand. Here's my test code:
Dim w As XmlWriter = XmlWriter.Create("Out.xml") w.WriteStartDocument() w.WriteDocType("SOME TEXT", Nothing, Nothing, "DTDName.dtd") w.Close()
I've tried a number of variations on that including """DTDName.dtd""". What do I have to do to get this one simple line of xml out to the file? <!DOCTYPE SOME TEXT "DTDName.dtd">
> > (I would have to translate the DTD manually, right? There's no wizard or > > utility for such a conversion? If I am not mistaken, DTDs obsolete and > > there's little or no support for them in Dot Net?)
B. Chernick wrote: > Dim w As XmlWriter = XmlWriter.Create("Out.xml") > w.WriteStartDocument() > w.WriteDocType("SOME TEXT", Nothing, Nothing, "DTDName.dtd")
Well the first argument is the root element name and an element name is not allowed to contain a space so you need to decide which root element name you want (e.g. "some-name" or "some_name") and write that out. You can't use "SOME TEXT" as the space is not allowed.
The actual sample xml I was originally given has a line of:
<!DOCTYPE BDD SYSTEM "EAI.dtd">
Are you saying that this violates XML standards in general?
(Actually I should explain that for some reason I was convinced that the last parm of the line was the problem. I didn't realize it was the first. This works: w.WriteDocType("BDD-SYSTEM", Nothing, Nothing, "EAI.dtd")
but results in <!DOCTYPE BDD-SYSTEM [EAI.dtd]> Why the brackets and is there any way to get rid of them?)
> > Dim w As XmlWriter = XmlWriter.Create("Out.xml") > > w.WriteStartDocument() > > w.WriteDocType("SOME TEXT", Nothing, Nothing, "DTDName.dtd")
> Well the first argument is the root element name and an element name is > not allowed to contain a space so you need to decide which root element > name you want (e.g. "some-name" or "some_name") and write that out. You > can't use "SOME TEXT" as the space is not allowed.
B. Chernick wrote: > The actual sample xml I was originally given has a line of:
> <!DOCTYPE BDD SYSTEM "EAI.dtd">
> Are you saying that this violates XML standards in general?
No, it does not. The "BDD" is the root element name and "EAI.dtd" is the system identifier.
> (Actually I should explain that for some reason I was convinced that the > last parm of the line was the problem. I didn't realize it was the first. > This works: w.WriteDocType("BDD-SYSTEM", Nothing, Nothing, "EAI.dtd")
> but results in <!DOCTYPE BDD-SYSTEM [EAI.dtd]> Why the brackets and is > there any way to get rid of them?)
Use w.WriteDocType("BDD", Nothing, "EAI.dtd", Nothing) to produce the line you posted above. The "SYSTEM" string will be automatically added as you pass in a third argument string, the system identifier. See also the online documentation http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.writedoc... or your local MSDN copy which explains the four arguments.