0

是否有可能使用 XML 提要中的特定字段设置新的 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>
4

1 回答 1

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();
于 2013-05-21T08:49:44.563 回答