这个想法是我需要从我的网络服务中返回一个公告列表和一个视频列表作为字符串。我在将列表放入 XML 架构时遇到问题。
这是我的 XML 架构:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="WelcomeScreenSchema"
elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:attribute name="ID" type="xs:int"/>
<xs:attribute name="Added" type="xs:dateTime"/>
<xs:element name="WelcomeScreen">
<xs:complexType>
<xs:all>
<xs:element ref="Announcements" maxOccurs="1" minOccurs="1" />
<xs:element ref="Videos" maxOccurs="1" minOccurs="1" />
</xs:all>
</xs:complexType>
</xs:element>
<xs:element name="Announcements">
<xs:complexType>
<xs:sequence>
<xs:element ref="Announcement" maxOccurs="unbounded" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Videos">
<xs:complexType>
<xs:sequence>
<xs:element ref="Video" maxOccurs="unbounded" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Announcement">
<xs:complexType>
<xs:attribute ref="ID" use="required"/>
<xs:attribute ref="Added" use="required"/>
<xs:attribute name="HTML" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="Video">
<xs:complexType>
<xs:attribute ref="ID" use="required"/>
<xs:attribute ref="Added" use="required"/>
<xs:attribute name="URL" type="xs:string" use="required"/>
<xs:attribute name="Title" type="xs:string" use="required"/>
<xs:attribute name="Description" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
我正在使用以下 C# 填充我的数据:
XmlObject result = new XmlObject("C:\path\to\schema.xsd");
result.Add(new XElement("WelcomeScreen"));
result.Root.Add(new XElement("Announcements"));
result.Root.Add(new XElement("Videos"));
result.Root.Element("Announcements").Add(new XElement("Announcement",
new XAttribute("ID", Convert.ToString(id)),
new XAttribute("HTML", html),
new XAttribute("Added", added)
));
result.Root.Element("Videos").Add(new XElement("Video",
new XAttribute("ID", Convert.ToString(id)),
new XAttribute("HTML", URL),
new XAttribute("HTML", Title),
new XAttribute("HTML", Description),
new XAttribute("Added", added)
));
我添加Announcement
和Video
元素的行实际上是在我从数据库中读取信息的循环内,但为了简洁起见,我排除了该代码。
它将三个元素加载Announcement
到模式中没有问题。当它尝试加载第一个Video
元素时,它会崩溃并出现以下错误:
System.Exception was unhandled
HResult=-2146233088
Message=Error getting Welcome Screen info from DB. : System.InvalidOperationException: Duplicate attribute.
at System.Xml.Linq.XElement.AddAttributeSkipNotify(XAttribute a)
at System.Xml.Linq.XContainer.AddContentSkipNotify(Object content)
at System.Xml.Linq.XContainer.AddContentSkipNotify(Object content)