XML System.InvalidOperationException:属性重复

本文关键字:属性 System InvalidOperationException XML | 更新日期: 2023-09-27 18:22:08

我的想法是,我需要从web服务中以字符串的形式返回一个公告列表和一个视频列表。我在将列表放入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)
));

我添加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)

XML System.InvalidOperationException:属性重复

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

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)

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

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

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

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

你可能想这样写

new XAttribute("URL", URL),
new XAttribute("Title", Title),
new XAttribute("Description", Description),
相关文章: