简体   繁体   English

如何使用.net Web服务创建自定义xml标签?

[英]How to create custom xml tags using .net web services?

I am using LINQ-To-SQL for database. 我正在使用LINQ-To-SQL数据库。

I am returning the xml content from my web service as follow: 我从Web服务返回xml内容,如下所示:
The code: 编码:

DataClassesDataContext dc = new DataClassesDataContext();
[WebMethod]
    public List<Books> getBooks()
    {
        return dc.Books.ToList();    
    }

Output: 输出:

<ArrayOfBook>
−
<Book>
<bookID>1</bookID>
<title>Programming with Java</title>
<author>Balagurusami</author>
<summary>Summary1</summary>
</Book>
−
<Book>
<bookID>2</bookID>
<title>Programming with C</title>
<author>M.M.Patel</author>
<summary>Summary2</summary>
</Book>
−
<Book>
<bookID>3</bookID>
<title>ASP in 21 Days</title>
<author>K.J. Malai</author>
<summary>Summary3</summary>
</Book>
−
<Book>
<bookID>4</bookID>
<title>Book Title</title>
<author>autho1</author>
<summary>summary4</summary>
</Book>
</ArrayOfBook>

Question: 题:

How can I put custom attributes & nodes in my output? 如何在输出中放置自定义属性和节点?

Is the XML for human consumption? XML是否供人类使用? This is one of those areas where developers get stuck in a rut--they have this perception of what the XML "should" look like, and spend countless hours messing around with attributes and IXmlSerializable in order to get it to look the way they want. 这是开发人员陷入困境的领域之一-他们对XML“应该”的外观有这种认识,并花了无数的时间弄乱属性和IXmlSerializable,以使其看起来像他们想要的样子。 After all that wasted time they then sadly realize their application runs no differently than it did when they let the damn framework serialize their classes the way XmlSerializer wanted to. 在所有这些浪费的时间之后,他们然后遗憾地意识到,他们的应用程序的运行与让该死的框架按照XmlSerializer想要的方式序列化其类时的运行没有什么不同。

Advice: Don't do it. 忠告:不要这样做。 Walk away. 走开。 Work on something you actually need to work on. 处理您实际需要处理的事情。

Wait, you're still reading this? 等一下,您还在阅读吗? Oh well, guess you have to learn somehow. 哦,好吧,你必须学习一些方法。

Two options: Implement IXmlSerializable or use attributes on your properties. 两个选项:实现IXmlSerializable或在属性上使用属性。 I would strongly suggest you go with option two. 我强烈建议您选择第二种方法。 With the interface, you must construct and parse the xml yourself, which is harder than it sounds. 使用该接口,您必须自己构造和解析xml,这比听起来要难。 This is even with the advent of the linq to xml classes (XElement etal). 甚至随着linq to xml类(XElement等)的出现。 I've done that before and it ate up a week of my time dealing with this or that little niggling issue. 我之前已经做过,并且花了我一周的时间来处理这个或那个小问题。

Attributes such as XmlAttribute or XmlElement are the quickest route to instruct the xml serializer how to generate your class' XML. 诸如XmlAttributeXmlElement之类的属性是指示xml序列化程序如何生成类的XML的最快途径。 There are some situations (like collection properties) where you will find it hard to massage the xml using attributes the way you want it (smirk). 在某些情况下(例如集合属性),您会发现很难使用所需的属性(傻笑)来对xml进行处理。

So attributes are the definite better option. 因此,属性无疑是更好的选择。 Unfortunately for you , you're serializing linq to sql entities--generated code. 不幸的是 ,您正在将linq序列化为sql实体生成的代码。 You can edit the generated code to add the attributes, but now you're screwed whenever you have to regenerate. 您可以编辑生成的代码以添加属性,但是现在,无论何时必须重新生成,您都一头雾水。 Are you starting to get the feeling you shouldn't worry so much about what your xml looks like? 您是否开始感到不必为xml的外观担心太多?

So, in your case, you have no option but to add partial classes for each of your entities and implement IXmlSerializable You can construct your xml many different ways (using 2.0 classes, linq to xml classes, or by using freaking StringBuilder). 因此,在您的情况下,您别无选择,只能为每个实体添加部分类并实现IXmlSerializable。可以用多种不同的方式构造xml(使用2.0类,linq to xml类或使用怪异的StringBuilder)。 I'd suggest concentrating on the linq to xml classes. 我建议专注于linq to xml类。 If you really want to abuse yourself this way. 如果您真的想这样虐待自己。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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