开发者

Creating Simple Xml In C#

I need help some help printing out some xml. Here is my code (which isnt working its not even formatting the full url right, it doesnt understand "//" in the url string) It also doesnt understand "<". There must be a better way to do this??

 foreach (string url in theUrls)
         {
             fullurl=@"http://www.cambit.com/restaurants" +url;
             xml = xml + @"<url>" + Environment.NewLine +
                       @"<loc>" + fullurl + @"</loc>" + Environment.NewLine +
                       @"<changefreq>weekly</changefreq>" + Environment.NewLine +
                       @"<priority>0.80</priority>" + Environment.NewLine +
                       @"</url>" + Environment.NewLine;    

        }

It returns 400 of these appended right next to each other. Environment.NewLine isn't working either....

http://www.cambit.com/restaurantsBerwyn weekly开发者_StackOverflow 0.80

I tried this and it says the loc object is not set to an instance of an object

 XmlDocument aNewNode = new XmlDocument();
 XmlElement urlRoot = aNewNode.CreateElement("url");
 //aNewNode.DocumentElement.AppendChild(urlRoot);
 XmlElement loc = aNewNode.CreateElement("loc");
 XmlText locText = aNewNode.CreateTextNode(fullurl);
 aNewNode.DocumentElement.AppendChild(loc);
 aNewNode.DocumentElement.LastChild.AppendChild(locText);
 XmlElement chgFreq = aNewNode.CreateElement("changefreq");
 XmlText chgFreqText = aNewNode.CreateTextNode("weekly");
 aNewNode.DocumentElement.AppendChild(chgFreq);
 aNewNode.DocumentElement.LastChild.AppendChild(chgFreqText);
 XmlElement priority = aNewNode.CreateElement("priority");
 XmlText priorityText = aNewNode.CreateTextNode("0.80");
 aNewNode.DocumentElement.AppendChild(priority);
 aNewNode.DocumentElement.LastChild.AppendChild(priorityText);

What am doing wrong??


One of the easiest ways to do this is to use XDocument, which has lots of documentation. Here's an example from the documentation:

XDocument srcTree = new XDocument(
    new XComment("This is a comment"),
    new XElement("Root",
        new XElement("Child1", "data1"),
        new XElement("Child2", "data2"),
        new XElement("Child3", "data3"),
        new XElement("Child2", "data4"),
        new XElement("Info5", "info5"),
        new XElement("Info6", "info6"),
        new XElement("Info7", "info7"),
        new XElement("Info8", "info8")
    )
);

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

The way this would work for your example would be something like:

public XDocument CreateDocument(IEnumerable<string> theUrls)
{
    var urlElements = theUrls.Select(u => CreateUrlElement(u));
    return new XDocument(new XElement("Urls", urlElements));
}

public XElement CreateUrlElement(string url)
{
    return new XElement("Url",
        new XElement("loc", fullUrl),
        ... the rest of your elements ...);
}


Use XDocument. There is a nice example on that website.


you probably should use CDATA section

http://en.wikipedia.org/wiki/CDATA


You can use XMLWriter class, here is example. Or maybe better LINQ To XML

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜