在 ASP.NET 中动态(内存)创建 XML 并在浏览器中显示

发布于 2024-11-30 09:26:29 字数 1816 浏览 0 评论 0原文

我正在尝试创建一个 API XML,其中包含来自 MySQL 数据库的数据。我正在努力实现如下所示的目标。

<api xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <key>eee83d24-906b-4736-91d9-1031621b79eb</key>
    <name>API Test</name>
    <feedState>Test</feedState>
    <news href="http://api.mydomain.com/eee83d24-906b-4736-91d9-1031621b79eb/news/"/>
    <comments href="http://api.mydomain.com/eee83d24-906b-4736-91d9-1031621b79eb/comments/"/>
</api>

如果我打开浏览器并输入以下 URL,我可以看到上面的 XML

http://api.mydomain.com/eee83d24-906b-4736-91d9-1031621b79eb

现在,如果我需要查看“新闻”中的详细信息,我现在只需将浏览器指向,

http://api.mydomain.com/eee83d24-906b-4736-91d9-1031621b79eb/news 

完成此操作后,我将看到类似“

<news xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" state="Trial">
 <newsListItem href="http://api.mydomain.com/eee83d24-906b-4736-91d9-1031621b79eb/news/800577419/">
   <id>800577419</id>
   <publishDate>2011-08-17</publishDate>
   <lastModifiedDate>2011-08-17</lastModifiedDate>
   <headline>Google buys Motorola</headline>
</newsListItem>
<newsListItem href="http://api.mydomain.com/eee83d24-906b-4736-91d9-1031621b79eb/news/800577416/">
   <id>800577416</id>
   <publishDate>2011-08-17T15:13:12</publishDate>
   <lastModifiedDate>2011-08-17T15:14:36</lastModifiedDate>
   <headline>Apple Macbook Air</headline>
 </newsListItem>
</news>

记住所有数据 ”的内容上图来自MySQL数据库。

我怎样才能实现上述目标?欢迎任何示例、想法。如果有人能点亮我将不胜感激。 WCF 是出路吗?或者使用普通的旧 ASP.NET XDoc 等...?

请指教。非常感谢!

I am trying to create an API XML which has data coming from MySQL database. I am trying to acheive something like below.

<api xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <key>eee83d24-906b-4736-91d9-1031621b79eb</key>
    <name>API Test</name>
    <feedState>Test</feedState>
    <news href="http://api.mydomain.com/eee83d24-906b-4736-91d9-1031621b79eb/news/"/>
    <comments href="http://api.mydomain.com/eee83d24-906b-4736-91d9-1031621b79eb/comments/"/>
</api>

I can see the above XML if I open my browser and type in the below URL

http://api.mydomain.com/eee83d24-906b-4736-91d9-1031621b79eb

Now if I need to see details within "news" I will simple point my browser now to,

http://api.mydomain.com/eee83d24-906b-4736-91d9-1031621b79eb/news 

After doing that I will see something like,

<news xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" state="Trial">
 <newsListItem href="http://api.mydomain.com/eee83d24-906b-4736-91d9-1031621b79eb/news/800577419/">
   <id>800577419</id>
   <publishDate>2011-08-17</publishDate>
   <lastModifiedDate>2011-08-17</lastModifiedDate>
   <headline>Google buys Motorola</headline>
</newsListItem>
<newsListItem href="http://api.mydomain.com/eee83d24-906b-4736-91d9-1031621b79eb/news/800577416/">
   <id>800577416</id>
   <publishDate>2011-08-17T15:13:12</publishDate>
   <lastModifiedDate>2011-08-17T15:14:36</lastModifiedDate>
   <headline>Apple Macbook Air</headline>
 </newsListItem>
</news>

Remember all the data shown above comes from MySQL Database.

How can I achieve the above? Any examples samples, ideas are welcome. I would be obliged if someone can throw a light. Is WCF the way to go? or using plain old ASP.NET XDoc etc...?

Please advise. Thanks a ton!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゃ人海孤独症 2024-12-07 09:26:29

您可以选择使用 WCF REST 服务并轻松返回 XML。只需使用此处找到的 Visual Studio 项目模板创建一个项目。
如果您已经定义了从 MySQL 数据库读取数据的类,则可以返回它们并让 WCF 为您序列化它们。这样您就不必使用 XDoc/XElement 来构建 XML。
方法和服务类看起来类似于:

[ServiceContract]
[XmlSerializerFormat]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Single)]
public class APIService
{
  [WebGet(UriTemplate = "{key}", ResponseFormat = WebMessageFormat.Xml)]
  public APIData GetAPIData(string key)
  {
    // build apiData object from DB using "key"
    return apiData
  }

  [WebGet(UriTemplate = "{key}/news", ResponseFormat = WebMessageFormat.Xml)]
  public APINewsData GetAPINewsData(string key)
  {
    // build apiNewsData object from DB using "key"
    return apiNewsData
  }


}

如果您使用 XmlSerializer 类(通过像上面一样指定 [xmlSerializerFormat] 属性)而不是 DataContractSerializer 类(默认 WCF 序列化程序),您可以更好地控制返回 XML 的外观。但它比默认选项慢。更多详细信息: http://msdn.microsoft.com/en-us/library/ ms733901.aspx

You can choose to use WCF REST service and return your XML easily. Simply create a project using Visual Studio Project template found here.
If you already have classes defined that reads data from MySQL database, you can return them and have WCF serialize it for you. This way you dont have to use XDoc/XElement to build XML.
The method and service class would look something like:

[ServiceContract]
[XmlSerializerFormat]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Single)]
public class APIService
{
  [WebGet(UriTemplate = "{key}", ResponseFormat = WebMessageFormat.Xml)]
  public APIData GetAPIData(string key)
  {
    // build apiData object from DB using "key"
    return apiData
  }

  [WebGet(UriTemplate = "{key}/news", ResponseFormat = WebMessageFormat.Xml)]
  public APINewsData GetAPINewsData(string key)
  {
    // build apiNewsData object from DB using "key"
    return apiNewsData
  }


}

If you use XmlSerializer class (by specifying [xmlSerializerFormat] attribute like above) instead of DataContractSerializer class (default WCF serializer) you can have more control on how the return XML looks. But it is slower than the default option. More details: http://msdn.microsoft.com/en-us/library/ms733901.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文