简体   繁体   中英

C# Reproducing RSS feed

I have made a program that scans rss feeds. This same program creates feeds from elements it has crawled. This means that the rss feeds are not identical, but the items must be. It copies it. It is therefore essential that what comes out is the same thing that comes in.

Now, there are occurences where elmenents in the input rss's has elements with names like this:

<dc:creator>tomatoes</dc:creator>

Now, when i scan this it works perfectly. The element get saved to database and everything is jolly good.

When i try to write it out again to an RSS feed, using these codelines (and a bunch of foreaches, if's +++)

StringBuilder sb = new StringBuilder();
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = "  ";
settings.NewLineOnAttributes = true;
XmlWriter feedWriter = XmlWriter.Create(sb, settings);

And this line for each element:

feedWriter.WriteElementString(keyAndValue[0], keyAndValue[1]);

I get this error message if i hit the example element above:

Invalid name character in 'dc:creator'. The ':' character, hexadecimal value 0x3A, cannot be included in a name.

Now, i have found a lot of articles where this error has been mentioned. And in almost all of them they questioneer is told that this is not correct XML, and should drop writing the ':'. I however can't.

I found one example where you could use another overloaded method of XmlWriter, this one:

feedWriter.WriteElementString(prefixAndKey[0],prefixAndKey[1],"Namespace",keyAndValue[1]);

However this causes the element to look like this:

<dc:creator xmlns:something="NameSpace">tomatoes</dc:creator>

This is, as you all know not the same as the one above because it contains the xmlns bit.

I also tried another 'hack' which would work as follows:

StringBuilder sb = new StringBuilder();
StringWriter stringWriter = new StringWriter(sb);
XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter);

and

feedWriter.WriteElementString(keyAndValue[0], keyAndValue[1]);

This built and did not return errors, but when i opend it in Firefox, it displayed 0 items.

I then took a closer look at the feed i was getting this elements from, and it contained a rss element like this:

<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">

I am currently trying to replicate this.

Is there a reason why this might work? why? Is there an easier way to do this? Do i have to add a xmlns:dc or xmlns:itunes or whatever tag for all the different kinds of tags there is out there?

I need a simple and secure way of dealing with this, no matter what comes in the input rss feeds.

A quick snippet with XDocument:

    XNamespace dc = @"http://purl.org/dc/elements/1.1/";

    XElement doc = new XElement("items", 
        new XAttribute(XNamespace.Xmlns + "dc", dc),
        new XElement("item",
          new XElement("title", "test"),
          new XElement(dc + "creator", "tomatoes"))) ;

Gives

<items xmlns:dc="http://purl.org/dc/elements/1.1/">
  <item>
    <title>test</title>
    <dc:creator>tomatoes</dc:creator>
  </item>
</items>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM