在 ASP.NET 中动态(内存)创建 XML 并在浏览器中显示
我正在尝试创建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以选择使用 WCF REST 服务并轻松返回 XML。只需使用此处找到的 Visual Studio 项目模板创建一个项目。
如果您已经定义了从 MySQL 数据库读取数据的类,则可以返回它们并让 WCF 为您序列化它们。这样您就不必使用 XDoc/XElement 来构建 XML。
方法和服务类看起来类似于:
如果您使用 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:
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