#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Ukraine
    Posts
    5
    Rep Power
    0

    Question How to create xml on a fly


    Can some body to help with xml tree creation on a fly. How can I add node?

    Thanks.
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    1
    Rep Power
    0
    What environment are you working in?

    I'm not the most experienced .NET programmer you'll find around here, I'm sure, but I'd start with the XmlDataDocument object documentation, and grow from there. I designed a fairly simple schema and a program to update a file based on that schema with very litte trouble in VS.NET, and I hadn't worked with it before...

    I'd get very familiar with the documentation and how to find stuff that you need... I'm not quite there yet, but I'm learning. :)

    (I'm mostly a web guy, but have been doing some RAD lately as well.)
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Location
    Bavaria, Germany
    Posts
    140
    Rep Power
    12
    System.Xml.XmlDocument/MSXML2.DOMDocument

    myDoc.LoadXML(String xml):
    loads a xml document out of an string

    myDoc.Load(String filename);
    loads a xml doc from a file

    myDoc.Save(String filename);
    saves a xml doc to a file

    ---

    myDoc.CreateElement(String name)
    myDoc.CreateAttribute(String name)
    myDoc.CreateComment(String cm)
    ...
    Creates the different types of nodes

    ---

    Output is different between MSXML4 and MSXML.Net:

    MSXML4: myDoc.xml
    MSXML.Net: myDoc.OuterXml

    Returns this doc as String

    Attributes:
    myNode.setAttribute(name, value);
    myNode.getAttribute(name);

    Nodes:
    myNode.AppendChild(node);
    myNode.ReplaceChild(new, old);
    myNode.InsertAfter(new, ref); // only MSXML.Net
    myNode.InsertBefore(new, ref);

    and for node navigation:

    myNode.nextSibling: returns the node right in the list
    myNode.previousSibling: returns the node left in the list
    myNode.parentNode: returns the parent node
    myNode.childNodes: returns all child nodes

    Example C#:

    XmlDocument myDoc = new XmlDocument();

    XmlElement myEle = myDoc.CreateElement("root");
    for (int i = 0; i < 3; i++) {
    XmlElement myChild = myDoc.CreateElement("child");
    myChild.SetAttribute("index", i);
    myEle.AppendChild(myChild);
    }

    Console.WriteLine(myEle.OuterXml);

    -> output:
    <root><child index="1" /><child index="2" /><child index="3" /></root>

    Example VB6:

    Dim myDoc As DOMDocument
    Set myDoc = new DOMDocument

    Dim myEle As IXMLDOMElement
    Dim myChild As IXMLDOMElement

    Set myEle = myDoc.CreateElement("root")

    Dim i As Long
    For i = 0 To 3
    Set myChild = myDoc.CreateElement("child")
    myChild.SetAttribute "index", i
    myEle.AppendChild(myChild)
    Next

    Debug.Print myDoc.xml

    --> output same as above.

    See? It's very easy once you get the main methods since it's always the same (and btw - for the DHTML coders out there...doesn't this remind you on something? ;-) the javascript/html doc is *exactly* the same thing)
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Ukraine
    Posts
    5
    Rep Power
    0

    Great explanation


    Thanks a lot!
    This is a greatest explanation I could expact!

IMN logo majestic logo threadwatch logo seochat tools logo