tags:

views:

178

answers:

8

i have not worked with XML in a while can someone post the syntax needed to build and save an xml node structure that resembles that of a tree structure that is created by a recursive function.

basically i have a recursive function that saves data found an a page(url) and then follows each URL found on that page recursivley and does the same to it. to audit this i want an output as a xml file to disk so i can see how it is doing its recursion and parsing.

the code i have is below. please add to it the needed xml calls needed to create a xml structure like the one i show below. (please include the full name space so i can see where the objects i need in .net are for this.)

page1.htm has 2 links on it:

<a href=page1_1.htm>
<a href=page1_2.htm>

page1_1.htm has 2 links on it

<a href=page1_1_a.htm> (this then will have some links also)
<a href=page1_1_b.htm> (this then will have no more links on it - dead end)

the xml should do something like this:

<node url=page1.htm>
    ...<node url=page1_1_a.htm>
       ......    <node url="xxx.htm"/>
       ......    <node url="yyy.htm".>
    ... </node>
    ...<node url=page1_1_b.htm />

</node>
+1  A: 

Google is your Friend!

now do some reading!

XDocument

fampinheiro
+1  A: 

Another option is to use XML Serialization to do the conversion for you if you already have an object tree in place.

unholysampler
+1  A: 

Try this:

Creating a XML Document from scratch without using a file in C#

or this:

Manually building an XmlDocument

Either should give you the examples you are looking for

NSA
A: 

The objects you want are in the System.Xml namespace.

XmlDocument doc = new XmlDocument();

doc.AppendChild(CreateNodeElement(doc, rootNode));

doc.Save(fileName);

...

private XmlElement CreateNodeElement(XmlDocument doc, Node node)
{
    XmlElement output = doc.CreateElement("node");

    output.Attributes.Append(doc.CreateAttribute("url")).Value = node.Url;

    foreach(Node child in node.Links)
    {
        output.AppendChild(CreateNodeElement(doc, child));
    }

    return output;
}
Adam Robinson
This is a perfectly valid answer, but the new `XDocument` set of classes in `System.Xml.Linq` is a nice, new, cleaner way to create XML.
Matt Greer
+2  A: 

For start up try the following name space, it has all the classes for xml generation and manipulation

  • System.Xml Namespace

    XmlTextWriter xWriter = new XmlTextWriter(Console.Out);    
    xWriter.WriteStartElement("prefix", "Element1", "namespace"); 
    xWriter.WriteStartAttribute("prefix", "Attr1", "namespace1"); 
    xWriter.WriteString("value1"); 
    xWriter.WriteStartAttribute("prefix", "Attr2", "namespace2"); 
    xWriter.WriteString("value2"); 
    xWriter.Close();
    

If you are familiar with LINQ, have a look at

here is a sample

XDocument srcTree = new XDocument(
    new XComment("This is a comment"),
    new XElement("Root",
        new XElement("Child1", "data1"),
        new XElement("Child2", "data2"),        
    )
);

XDocument doc = new XDocument(
    new XComment("This is a comment"),
    new XElement("Root",
        from el in srcTree.Element("Root").Elements()
        where ((string)el).StartsWith("data")
        select el
    )
);
Console.WriteLine(doc);
Asad Butt
A: 

If you are using 3.5 or higher, use the latest and greatest XDocument class. It'd make you fall in love!

KMan
+1  A: 

I have this XML document to be generated on the fly at run-time.

<?xml version="1.0" encoding="utf-8"?>
<wap-provisioningdoc>
  <characteristic type="BOOTSTRAP">
    <parm name="NAME" value="SYNCSETTINGS" />
  </characteristic>
  <characteristic type="APPLICATION">
    <parm name="APPID" value="w5" />
    <parm name="TO-NAPID" value="INTERNET" />
    <parm name="NAME" value="SYNCSETTINGS" />
    <parm name="ADDR" value="http://syncserver/sync" />
    <characteristic type="RESOURCE">
      <parm name="URI" value="pb" />
      <parm name="NAME" value="Contacts DB" />
      <parm name="AACCEPT" value="text/x-vcard" />
    </characteristic>
    <characteristic type="RESOURCE">
      <parm name="URI" value="cal" />
      <parm name="NAME" value="Calendar DB" />
      <parm name="AACCEPT" value="text/x-vcalendar" />
    </characteristic>
    <characteristic type="RESOURCE">
      <parm name="URI" value="notes" />
      <parm name="NAME" value="Notes DB" />
      <parm name="AACCEPT" value="text/plain" />
    </characteristic>
    <characteristic type="APPAUTH">
      <parm name="AAUTHNAME" value="username" />
      <parm name="AAUTHSECRET" value="password" />
    </characteristic>
  </characteristic>
</wap-provisioningdoc>

This is how I generated this XML document using C# 3.0 and Linq.

public string CreateOTAXmlFile(string Username, string Password)
    {
        var ota = new XDocument(
                    new XElement("wap-provisioningdoc",
                        new XElement("characteristic", new XAttribute("type", "BOOTSTRAP"),
                            new XElement("parm", new XAttribute("name", "NAME"), new XAttribute("value", "SYNCSETTINGS"))
                                    ),
                        new XElement("characteristic", new XAttribute("type", "APPLICATION"),
                            new XElement("parm", new XAttribute("name", "APPID"), new XAttribute("value", "w5")),
                            new XElement("parm", new XAttribute("name", "TO-NAPID"), new XAttribute("value", "INTERNET")),
                            new XElement("parm", new XAttribute("name", "NAME"), new XAttribute("value", "SYNCSETTINGS")),
                            new XElement("parm", new XAttribute("name", "ADDR"), new XAttribute("value", "http://syncserver/sync")),
                            new XElement("characteristic", new XAttribute("type", "RESOURCE"),
                                new XElement("parm", new XAttribute("name", "URI"), new XAttribute("value", "pb")),
                                new XElement("parm", new XAttribute("name", "NAME"), new XAttribute("value", "Contacts DB")),
                                new XElement("parm", new XAttribute("name", "AACCEPT"), new XAttribute("value", "text/x-vcard"))
                                        ),
                            new XElement("characteristic", new XAttribute("type", "RESOURCE"),
                                new XElement("parm", new XAttribute("name", "URI"), new XAttribute("value", "cal")),
                                new XElement("parm", new XAttribute("name", "NAME"), new XAttribute("value", "Calendar DB")),
                                new XElement("parm", new XAttribute("name", "AACCEPT"), new XAttribute("value", "text/x-vcalendar"))
                                        ),
                            new XElement("characteristic", new XAttribute("type", "RESOURCE"),
                                new XElement("parm", new XAttribute("name", "URI"), new XAttribute("value", "notes")),
                                new XElement("parm", new XAttribute("name", "NAME"), new XAttribute("value", "Notes DB")),
                                new XElement("parm", new XAttribute("name", "AACCEPT"), new XAttribute("value", "text/plain"))
                                        ),
                            new XElement("characteristic", new XAttribute("type", "APPAUTH"),
                                new XElement("parm", new XAttribute("name", "AAUTHNAME"), new XAttribute("value", Username)),
                                new XElement("parm", new XAttribute("name", "AAUTHSECRET"), new XAttribute("value", Password))
                                        )
                                    )
                                )
                            );

        ota.Save(Server.MapPath("~/OTA/") + Username + ".xml");
        return (ota.ToString());

    }
this. __curious_geek
this is perfect, can i use just c# no link? i read about link but never used it. also i have visual studio 2005 so i don think i can use it without .net 3.0 and VS2008
kacalapy
I recommend you should start using VS2008/C#-3.0. C# 3.0 is a much smarter compared to C# 2.0 and I have had very gud results of using C# 3.0 in my projects. Code above is written using LINQ only. Go ahead and use C# 3.0 without a second thought.
this. __curious_geek