从类结构VB.NET向XML节点添加属性

时间:2016-05-26 08:40:36

标签: xml vb.net

我从VB.NET和asmx

生成XML

我定义了一个结构,然后在我的代码中填充它,然后通过XML将其返回给用户。有没有办法将属性放在节点上?

Structure Output
Dim First as string
Dim Second as string
End Structure

 <WebMethod()> _
    Public Function ProduceOutput(byval Test as string) as Output
Dim output as new Output
output.First="Hello"
output.Second="World"

ProduceOutput=output

End Function

这会产生

<Output>
<First>Hello</First>
<Second>World</Second>
</Output>

我如何制作

<Output>
    <First this="##valueoftest##">Hello</First>
    <Second>World</Second>
    </Output>

1 个答案:

答案 0 :(得分:0)

First属性创建自己的类。为要用作属性的属性定义XmlAttribute XmlText将被序列化为父元素的值

<XmlType>
Public Class FirstType

   <XmlAttribute> 
   Public Property This As String

   <XmlText> 
   Public Property Value As String

End Class

然后将Output.First更改为该类的类型

Public Class Output
    Public Property First As FirstType
End Class

结果:

<Output>
    <First This="ThisValue">FirstValue</First>
    <Second>SecondValue</Second>
</Output>

相同的逻辑也适用于Structure