這個想法是,我需要從我的web服務返回一個公告列表和視頻列表作爲字符串。我在將列表放入XML模式時遇到問題。XML System.InvalidOperationException:重複屬性
這裏是我的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)