![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | XML Document creation My apologies if this has already been covered, but searching this discussion group via google did not turn up anything helpful. Here is my dilemma. I want to be able to create an xml document from scratch. I do not want to create it by generating some giant blob of text because that offends my OO sensibilities (*nose up...sniff*). Instead I would like to generate it via the DOM. So I can do this: $now = [System.DateTime]::Now $x = [xml]"<report><created>$now</created><master>Master Sample</master></report>" $tests = $x.CreateElement("tests") $tests.set_InnerText("Tests run for this report") $x.report.AppendChild($tests) $x.save("C:\Data\Active\Scripts\WPS\XMLReporter\test.xml") and I get this as an xml file (pretend you don't see that tiny blob of text...I was feeling lazy): <report> <created>11/05/2007 14:20:59</created> <master>Master Sample</master> <tests>Tests run for this report</tests> </report> Everything is perfect. The problem arises from trying to add a child node to <tests>. When I run this code: $now = [System.DateTime]::Now $x = [xml]"<report><created>$now</created><master>Master Sample</master></report>" $tests = $x.CreateElement("tests") $tests.set_InnerText("Tests run for this report") $n = $x.CreateElement("test") $n.set_InnerText("Sample Test 1") $tests.AppendChild($n) $x.report.AppendChild($tests) $x.save("C:\Data\Active\Scripts\WPS\XMLReporter\test.xml") Then I get this for the file: <report> <created>11/05/2007 14:24:08</created> <master>Master Sample</master> <tests>Tests run for this report<test>Sample Test 1</test></tests> </report> While I realize that this is perfectly valid from an XML perspective, it will cause me headaches. I am trying to fight the good fight and move as much non-database stored data as possible to XML. One of the major resistances that I face is that raw xml files "Are too hard to read". If I could just give them a simple INI or TXT file everything would be fine. ![]() So in short, is there a way to get the child node to add itself in a nice pretty fashion using the DOM rather than building the entire doc as a big blob of text? |
My System Specs![]() |
| | #2 (permalink) |
| | Re: XML Document creation On Nov 5, 4:32 pm, ebgreen <ebgr...@xxxxxx> wrote: Quote: > My apologies if this has already been covered, but searching this discussion > group via google did not turn up anything helpful. > > Here is my dilemma. I want to be able to create an xml document from > scratch. I do not want to create it by generating some giant blob of text > because that offends my OO sensibilities (*nose up...sniff*). Instead I would > like to generate it via the DOM. So I can do this: > > $now = [System.DateTime]::Now > > $x = [xml]"<report><created>$now</created><master>Master > Sample</master></report>" > $tests = $x.CreateElement("tests") > $tests.set_InnerText("Tests run for this report") > $x.report.AppendChild($tests) > $x.save("C:\Data\Active\Scripts\WPS\XMLReporter\test.xml") > > and I get this as an xml file (pretend you don't see that tiny blob of > text...I was feeling lazy): > > <report> > <created>11/05/2007 14:20:59</created> > <master>Master Sample</master> > <tests>Tests run for this report</tests> > </report> > > Everything is perfect. The problem arises from trying to add a child node to > <tests>. When I run this code: > > $now = [System.DateTime]::Now > > $x = [xml]"<report><created>$now</created><master>Master > Sample</master></report>" > $tests = $x.CreateElement("tests") > $tests.set_InnerText("Tests run for this report") > > $n = $x.CreateElement("test") > $n.set_InnerText("Sample Test 1") > $tests.AppendChild($n) > > $x.report.AppendChild($tests) > $x.save("C:\Data\Active\Scripts\WPS\XMLReporter\test.xml") > > Then I get this for the file: > > <report> > <created>11/05/2007 14:24:08</created> > <master>Master Sample</master> > <tests>Tests run for this report<test>Sample Test 1</test></tests> > </report> > > While I realize that this is perfectly valid from an XML perspective, it > will cause me headaches. I am trying to fight the good fight and move as much > non-database stored data as possible to XML. One of the major resistances > that I face is that raw xml files "Are too hard to read". If I could just > give them a simple INI or TXT file everything would be fine. ![]() > > So in short, is there a way to get the child node to add itself in a nice > pretty fashion using the DOM rather than building the entire doc as a big > blob of text? I think the reason you aren't getting any answers to this thread is that you kinda neglected to fully-form your question. I'm going to take a stab at it nonetheless: I think you probably want to have the <test></test> nodes on a new line, nicely indented. There is a class in the FCL called XmlWriter which has a static method Create, which itself takes an output path (or stream) and an XmlWriterSettings object. This settings object denotes visual style for any output XML, among other things. You'll notice that there is an overload for your Save method that you call that takes an XmlWriter as a target. However, your real problem is the text node ( "tests run for this report" ) that is adjacent to your child <test /> nodes. AFAIK, there are no style rules that control this kind of content, e.g. I don't think you can programmatically force it onto a new line. I think your easiest route is to wrap this in a node itself, e.g. <title>Tests run for this report</title> ; you'll then end up with: ... <master>Master Sample</master> <tests> <title>Tests run for this report</title> <test>Sample Test 1</test></tests> <test>Sample Test 2</test></tests> <test>Sample Test 3</test></tests> </report> Or you could have title as an attribute of tests. Anyway, hope this helps. - Oisin / x0n http://oisin.powershell.com/ |
My System Specs![]() |
| | #3 (permalink) |
| | Re: XML Document creation Thanks a lot for the replay. Your guess at my root question is correct. Sorry for any confusion. I will play around with the new info that you have provided. Thanks again. "Oisin Grehan" wrote: Quote: > On Nov 5, 4:32 pm, ebgreen <ebgr...@xxxxxx> wrote: Quote: > > My apologies if this has already been covered, but searching this discussion > > group via google did not turn up anything helpful. > > > > Here is my dilemma. I want to be able to create an xml document from > > scratch. I do not want to create it by generating some giant blob of text > > because that offends my OO sensibilities (*nose up...sniff*). Instead I would > > like to generate it via the DOM. So I can do this: > > > > $now = [System.DateTime]::Now > > > > $x = [xml]"<report><created>$now</created><master>Master > > Sample</master></report>" > > $tests = $x.CreateElement("tests") > > $tests.set_InnerText("Tests run for this report") > > $x.report.AppendChild($tests) > > $x.save("C:\Data\Active\Scripts\WPS\XMLReporter\test.xml") > > > > and I get this as an xml file (pretend you don't see that tiny blob of > > text...I was feeling lazy): > > > > <report> > > <created>11/05/2007 14:20:59</created> > > <master>Master Sample</master> > > <tests>Tests run for this report</tests> > > </report> > > > > Everything is perfect. The problem arises from trying to add a child node to > > <tests>. When I run this code: > > > > $now = [System.DateTime]::Now > > > > $x = [xml]"<report><created>$now</created><master>Master > > Sample</master></report>" > > $tests = $x.CreateElement("tests") > > $tests.set_InnerText("Tests run for this report") > > > > $n = $x.CreateElement("test") > > $n.set_InnerText("Sample Test 1") > > $tests.AppendChild($n) > > > > $x.report.AppendChild($tests) > > $x.save("C:\Data\Active\Scripts\WPS\XMLReporter\test.xml") > > > > Then I get this for the file: > > > > <report> > > <created>11/05/2007 14:24:08</created> > > <master>Master Sample</master> > > <tests>Tests run for this report<test>Sample Test 1</test></tests> > > </report> > > > > While I realize that this is perfectly valid from an XML perspective, it > > will cause me headaches. I am trying to fight the good fight and move as much > > non-database stored data as possible to XML. One of the major resistances > > that I face is that raw xml files "Are too hard to read". If I could just > > give them a simple INI or TXT file everything would be fine. ![]() > > > > So in short, is there a way to get the child node to add itself in a nice > > pretty fashion using the DOM rather than building the entire doc as a big > > blob of text? > Hi Ebgreen, > > I think the reason you aren't getting any answers to this thread is > that you kinda neglected to fully-form your question. I'm going to > take a stab at it nonetheless: I think you probably want to have the > <test></test> nodes on a new line, nicely indented. There is a class > in the FCL called XmlWriter which has a static method Create, which > itself takes an output path (or stream) and an XmlWriterSettings > object. This settings object denotes visual style for any output XML, > among other things. You'll notice that there is an overload for your > Save method that you call that takes an XmlWriter as a target. > > However, your real problem is the text node ( "tests run for this > report" ) that is adjacent to your child <test /> nodes. AFAIK, there > are no style rules that control this kind of content, e.g. I don't > think you can programmatically force it onto a new line. I think your > easiest route is to wrap this in a node itself, e.g. <title>Tests run > for this report</title> ; you'll then end up with: > > ... > <master>Master Sample</master> > <tests> > <title>Tests run for this report</title> > <test>Sample Test 1</test></tests> > <test>Sample Test 2</test></tests> > <test>Sample Test 3</test></tests> > </report> > > Or you could have title as an attribute of tests. > > Anyway, hope this helps. > > - Oisin / x0n > http://oisin.powershell.com/ > > |
My System Specs![]() |
| | #4 (permalink) |
| | Re: XML Document creation "Oisin Grehan" <oising@xxxxxx> wrote in message news:1194301390.275637.47890@xxxxxx Quote: > However, your real problem is the text node ( "tests run for this > report" ) that is adjacent to your child <test /> nodes. AFAIK, there > are no style rules that control this kind of content, e.g. I don't > think you can programmatically force it onto a new line. within the same element. Quote: > I think your > easiest route is to wrap this in a node itself, e.g. <title>Tests run > for this report</title> ; you'll then end up with: .... <master>Master Sample</master> <tests title="Tests run for this report"> <test>Sample Test 1</test> <test>Sample Test 2</test> <test>Sample Test 3</test> </tests> .... -- Keith |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| display document creation date | Vista file management | |||
| accidentally moved a MS Word document,now it dissapears everytime i open any word document | Vista General | |||
| View and copy document content without opening the document | Vista General | |||
| View and copy document content without opening the document | Vista performance & maintenance | |||
| Creation of desktop shortcut for SharePoint document folerder | Vista networking & sharing | |||