Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 13th, 2004, 04:39 PM
Registered User
 
Join Date: Feb 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help formatting an XML document!!!

I'm beside myself, I've tried everything, nothing works. I"m using C# and taking an existing XML Document that has a comment line at the top, no schema ref. I wrote schema for these pre-made XML documents and all I want to do is programatically add a line at the top and bottom to add a schema ref (define the document element with an xsi reference –see below. I've tried everything. I can add nodes that are child, but not add a top level parent node, the following throws errors because the root is the first child, and I want to add a schema ref, which is the top level node! Plus its already an existing XML document so I’m not serializing a new one. I’m completely lost

XmlDocument doc = new XmlDocument();
            FileStream myFile = new FileStream(@"C:\ShippingOrder.xml", FileMode.Open);
            doc.Load(myFile);
            XmlNode root = doc.FirstChild;
            //create new node
            XmlElement elem = doc.CreateElement("Lio");
            elem.InnerText = "ThisWillbetheSchemaRef";
            //add node to the document
            root.InsertBefore(elem, root.FirstChild);


            myFile.Close();

Ie, I have a document Shipping:

<?xml version="1.0" encoding="ISO-8859-1"?>
    <shipTo>
        <name>Tom Jones</name>
        <address>555 windchester drive</address>
        <city>San Francisco</city>
        <state>CA</state>
        <country>United States</country>
    </shipTo>
    <items>
        <item>
            <title>Empires</title>
            <quantity>1</quantity>
            <price>50.00</price>
        </item>
        <item>
            <title>Desert Wines</title>
            <quantity>1</quantity>
            <price>10.90</price>
        </item>
        <item>
            <title>Cheese O' Rama</title>
            <quantity>1</quantity>
            <price>9.95</price>
        </item>
    </items>

and I want it to be:


<?xml version="1.0" encoding="ISO-8859-1"?>
<shipOrder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://localhost/XMLDemo/Shipping Order.xsd">
    <shipTo>
        <name>Tom Jones</name>
        <address>555 windchester drive</address>
        <city>San Francisco</city>
        <state>CA</state>
        <country>United States</country>
    </shipTo>
    <items>
        <item>
            <title>Empires</title>
            <quantity>1</quantity>
            <price>50.00</price>
        </item>
        <item>
            <title>Desert Wines</title>
            <quantity>1</quantity>
            <price>10.90</price>
        </item>
        <item>
            <title>Cheese O' Rama</title>
            <quantity>1</quantity>
            <price>9.95</price>
        </item>
    </items>
</shipOrder>

HELP!!!!


 
Old February 13th, 2004, 06:33 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Perhaps you can create two XmlDocument objects. One will contain your new root node, the other will contain the data stored in the existing file. Remember to close the file after you load it's contents into the XmlDocument object.

Then, just import the nodes from the second object under the new root node of the first. When you're done, dump the contents of the first object back into your existing file.


I haven't used C# before, but I'm surprised that C#'s XmlDocument can parse your original XML file since it's technically not valid XML -- your example contains two root nodes. Perhaps C# creates an implicit root node to contain malformed documents such as yours. If this is the case, you might be able to modify the properties of this root node.

Another possibility is to create a root node, and traverse the rest of the node structure and copy (clone) the existing node tree under your new root node, then delete the original nodes. I didn't see any methods in the C# XmlDocument API docs to suggest you can "move" a node, but it really wouldn't make sense to provide this functionality.

It does make sense, however, to throw an error when calling InsertBefore() on a root node, since that function creates a sibling node, and XML doesn't allow for multiple root nodes in a document.



Take care,

Nik
http://www.bigaction.org/
 
Old February 13th, 2004, 06:35 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

on a side note, I'm sure you're using the MSDN documentation, but in case you're not, you might want to read this link (and check out all it's neighbors in the Table of Contents:

  http://msdn.microsoft.com/library/de...classtopic.asp


Take care,

Nik
http://www.bigaction.org/





Similar Threads
Thread Thread Starter Forum Replies Last Post
formatting of a word document with vb.net krmilburn General .NET 0 April 18th, 2006 06:47 AM
XML - Special Character Formatting sean1230 XML 3 February 18th, 2004 05:17 AM
xml document androger XML 2 November 19th, 2003 05:52 PM
Writing an XML document Ben Horne XML 4 November 13th, 2003 12:22 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.