如何从XML feed设置新的XML文档

时间:2013-05-21 08:30:02

标签: c# xml twitter

是否有可能使用XML Feed中的特定字段设置新的XML文档?

public string xmlResult;
  public void Page_Load(object sender, EventArgs args){
    string url = string.Format("http://search.twitter.com/search.atom?q=test&show-user=true&rpp=10");

  HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

  using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
  {
    StreamReader reader = new StreamReader(response.GetResponseStream());
    xmlResult = reader.ReadToEnd();
    Console.WriteLine("OUTPUT: " + xmlResult);
  }
      XmlDocument doc = new XmlDocument();
      doc.LoadXml(xmlResult);
      Response.ContentType = "text/xml";
      Response.Write(doc.OuterXml);
}

我想创建一个包含特定字段的新XML文档,如下例所示。

<feed>
    <entry>
            <content></content>
            <author></author>
            <created_time></created_time>
    </entry>
</feed>

1 个答案:

答案 0 :(得分:1)

我发现LinqToXml更容易使用......

var xDoc = XDocument.Load("http://search.twitter.com/search.atom?q=test&show-user=true&rpp=10");
XNamespace ns = "http://www.w3.org/2005/Atom";

XElement feeds = new XElement("feed", 
                        xDoc.Descendants(ns + "entry")
                            .Select(e=>new XElement("entry",
                                                new XElement("id", e.Element(ns + "id").Value), 
                                                new XElement("author",e.Element(ns + "author").Element(ns + "name").Value),
                                                new XElement("created_time",e.Element(ns + "published").Value)
                                        )
                            )
                );

string xmlstr = feeds.ToString();