1

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 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??

2
  • When you say "printing" do you mean writing it to a printer, outputting to a webpage or what? Commented Mar 1, 2011 at 17:57
  • Possible duplicate of stackoverflow.com/questions/5042813/… Commented Mar 1, 2011 at 17:59

4 Answers 4

4

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 ...);
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

you probably should use CDATA section

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

1 Comment

i thought url part might corrupt xml and interpreted as corrupted.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.