Reputation: 2735
The idea is that I need to return a list of Announcements and a list of videos as a string from my web service. I'm having problems putting the lists into the XML Schema.
Here's my XML schema:
<?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>
I'm populating my data using the following 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)
));
The lines where I add the Announcement
and Video
elements, are actually inside loops where I'm reading info from my database, but I excluded that code for brevity.
It loads three Announcement
elements into the schema without a problem. When it tries to load the first Video
element, it crashes giving the following error:
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)
Upvotes: 0
Views: 5909
Reputation: 456
I think these attribute should have different name each of them and that's why it's complaining duplicate attribute...
new XAttribute("HTML", URL),
new XAttribute("HTML", Title),
new XAttribute("HTML", Description),
You probably want to write like this
new XAttribute("URL", URL),
new XAttribute("Title", Title),
new XAttribute("Description", Description),
Upvotes: 1
Reputation: 3558
You're trying to add three HTML
attributes in Video
. I think it should be:
result.Root.Element("Videos").Add(new XElement("Video",
new XAttribute("ID", Convert.ToString(id)),
new XAttribute("URL", URL),
new XAttribute("Title", Title),
new XAttribute("Description", Description),
new XAttribute("Added", added)
));
Upvotes: 1
Reputation: 1859
You are adding the same attribute multiple times, which is not allowed
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)
Upvotes: 2