XML System.InvalidOperationException:重复属性

时间:2013-07-11 14:39:24

标签: c# xml xsd

我的想法是,我需要从我的网络服务返回一个公告列表和一个视频列表作为字符串。我在将列表放入XML Schema时遇到了问题。

这是我的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)
));

我添加AnnouncementVideo元素的行实际上是在我从数据库中读取信息的内部循环中,但我为了简洁而排除了该代码。

它将三个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)

3 个答案:

答案 0 :(得分:2)

您要多次添加相同的属性,这是不允许的

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)

答案 1 :(得分:1)

您尝试在HTML中添加三个Video属性。我认为应该是:

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)
));

答案 2 :(得分:1)

我认为这些属性应该各有不同的名称,这就是为什么它抱怨重复的属性......

new XAttribute("HTML", URL),
new XAttribute("HTML", Title),
new XAttribute("HTML", Description),

你可能想写这样的

new XAttribute("URL", URL),
new XAttribute("Title", Title),
new XAttribute("Description", Description),