0

这个想法是我需要从我的网络服务中返回一个公告列表和一个视频列表作为字符串。我在将列表放入 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)
4

3 回答 3

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)
于 2013-07-11T14:46:37.840 回答
1

您正在尝试HTMLVideo. 我认为应该是:

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)
));
于 2013-07-11T14:47:01.033 回答
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),
于 2013-07-11T14:47:08.250 回答