开发者

How to generate and consume xml in C#?

i have not w开发者_如何学编程orked 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>


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

  • System.Xml.Linq Namespace

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);


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());

    }


Google is your Friend!

now do some reading!

XDocument


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


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;
}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜