以下列格式帮助序列化XML文件 [英] Help Serialize XML file in the following format
本文介绍了以下列格式帮助序列化XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,我有一个班级,我必须按以下格式序列化,我不知道我已经尝试谷歌搜索但没有找到所以我需要你的专家帮助:)
< XmlRoot( plist中</跨度>)GT; 公开 类 Plist
私有 _Elements() As Item = Nothing
< XmlArray(ElementName:= Elements)> _
公开 属性元素 As Item()
获取
返回 _Elements
结束 获取
设置(值 As Item())
_Elements = value
End 设置
结束 物业
公共 结构项目
< XmlElement( key)> _
Dim 键作为 字符串
< XmlElement( value)> _
Dim 值作为 字符串
公共 Sub 新(_ Key As String ,_ Value As 字符串)
Key = _Key
Value = _Value
End Sub
结束 结构
结束 Sub
并获得XML输出
<? xml version = 1.0 编码 = UTF-8 ? >
< plist xmlns:xsd = http://www.w3.org/2001/XMLSchema xmlns:xsi = http://www.w3.org/2001/XMLSchema-instance >
< 元素 >
< 项目 >
< key > key1 < span class =code-keyword>< / key >
< value > val < span class =code-keyword>< / value >
< / Item >
< 项目 >
< key > key2 < / key >
< value > 值< / value >
< / Item >
< / Elements >
< / plist >
但我希望它像
<? xml version = 1.0 < span class =code-summarycomment> encoding = UTF-8 ? >
< plist xmlns:xsd = http://www.w3.org/2001/XMLSchema xmlns:xsi = http://www.w3.org/2001/XMLSchema-instance >
< 元素 >
< key > key1 < / key >
< value > val < / value >
< key > key2 < / key >
< value > 值< / value >
< key < span class =code-keyword>> key3 < / key >
< value < span class =code-keyword>> value < / value >
< / Elements >
< / plist >
任何人都可以帮助我吗?
PS:对不起,如果我错了:(
解决方案
你想要生成的XML并不是很好d。除非您正在处理需要格式错误的现有XML导入代码,否则我将使用生成的XML。除了其他任何东西,它将使任何反序列化更简单。
如果你绝对_must_有一个格式错误的XML考虑使用XSL转换(或LINQ to XML或其他任何适合)转换自动生成的输出。如果你这样做,你需要一个反向转换来进行反序列化。
来自,https://en.wikipedia.org/wiki/Well-formed_document。
在其基础层面,格式良好的文件要求:
定义内容。
内容用开头和结尾标签分隔
内容可以正确嵌套(父母在根内,父母在子女内)
在这种情况下,最后一点是重要的一点。
PS。我不是专家。 :)
Hello everyone, well i have a class that i have to serialize in the following format which i don't know i've tried googling but nothing found so i needed some help from you experts :)
<XmlRoot("plist")> Public Class Plist
Private _Elements() As Item = Nothing
<XmlArray(ElementName:="Elements")> _
Public Property Elements As Item()
Get
Return _Elements
End Get
Set(value As Item())
_Elements = value
End Set
End Property
Public Structure Item
<XmlElement("key")> _
Dim Key As String
<XmlElement("value")> _
Dim Value As String
Public Sub New(_Key As String, _Value As String)
Key = _Key
Value = _Value
End Sub
End Structure
End Sub
and got the XML output as
<?xml version="1.0" encoding="UTF-8"?>
<plist xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Elements>
<Item>
<key>key1</key>
<value>val</value>
</Item>
<Item>
<key>key2</key>
<value>value</value>
</Item>
</Elements>
</plist>
But i want it like
<?xml version="1.0" encoding="UTF-8"?>
<plist xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Elements>
<key>key1</key>
<value>val</value>
<key>key2</key>
<value>value</value>
<key>key3</key>
<value>value</value>
</Elements>
</plist>
Anyone can help me ?
PS: Sorry if im wrong :(
解决方案
The XML you are trying to generate is not well formed. Unless you are dealing with existing XML import code that expects the badly formed layout I would use the generated XML. Apart from anything else it will make any deserialization simpler.
If you absolutely _must_ have the badly formed XML consider using an XSL transform (or LINQ to XML or whatever else suits) to convert the auto-generated output. If you do this you will need a reverse transform for deserializing.
From, https://en.wikipedia.org/wiki/Well-formed_document.
"At its base level well-formed documents require that:
Content be defined.
Content be delimited with a beginning and end tag
Content be properly nested (parents within roots, children within parents)"
The last point is the important one in this case.
PS. I am _not_ an expert. :)
这篇关于以下列格式帮助序列化XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文