LINQ to XML:Sequence不包含任何元素

时间:2011-05-25 11:39:11

标签: vb.net linq-to-xml

我有这个XML

   <?xml version="1.0" encoding="utf-8"?>
    <root>
        <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
        <xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
        <xsd:element name="root" msdata:IsDataSet="true">
      <xsd:complexType>
        <xsd:choice maxOccurs="unbounded">
          <xsd:element name="metadata">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="value" type="xsd:string" minOccurs="0" />
              </xsd:sequence>
              <xsd:attribute name="name" use="required" type="xsd:string" />
              <xsd:attribute name="type" type="xsd:string" />
              <xsd:attribute name="mimetype" type="xsd:string" />
              <xsd:attribute ref="xml:space" />
            </xsd:complexType>
          </xsd:element>
          <xsd:element name="assembly">
            <xsd:complexType>
              <xsd:attribute name="alias" type="xsd:string" />
              <xsd:attribute name="name" type="xsd:string" />
            </xsd:complexType>
          </xsd:element>
          <xsd:element name="data">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
                <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
              </xsd:sequence>
              <xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
              <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
              <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
              <xsd:attribute ref="xml:space" />
            </xsd:complexType>
          </xsd:element>
          <xsd:element name="resheader">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
              </xsd:sequence>
              <xsd:attribute name="name" type="xsd:string" use="required" />
            </xsd:complexType>
          </xsd:element>
        </xsd:choice>
      </xsd:complexType>
    </xsd:element>
   </xsd:schema>
  <resheader name="resmimetype">
    <value>text/microsoft-resx</value>
  </resheader>
  <resheader name="version">
    <value>2.0</value>
  </resheader>
  <resheader name="reader">
    <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
  </resheader>
  <resheader name="writer">
    <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
  </resheader>
  <data name="Nieuws" xml:space="preserve">
    <value>News</value>
  </data>
  <data name="Pagina's" xml:space="preserve">
    <value>Pages</value>
  </data>
  <data name="searchreasults" xml:space="preserve">
    <value>There were no matches found</value>
  </data>
</root>

我也有这个方法,我从中获取两个文件并从中获取一些值,然后我将所有值合并到第二个查询中的一个对象中:

Public Shared Function RetrieveTranslation(ByVal filefrom As String, ByVal fileto As String) As List(Of clsTranslation)
    Dim valuefrom = (From l In XElement.Load(fileto).Elements("data") Select l.Element("value").Value).FirstOrDefault

    Dim valuetrans = From vl In XElement.Load(filefrom).Elements("data") Select (New clsTranslation With {.Filename = filefrom, .Value = vl.Element("value").Value, .Valueto = valuefrom.ToString, .TranslationId = vl.Attribute("name").Value})
    Return valuetrans.ToList

End Function

所以,问题是当我运行代码时,第一个查询(valuefrom)中有一个“Sequence contains no elements”错误。我进行了调试,它说查询中没有任何内容,但我不明白为什么,“因为我做了类似的事情,从来没有像这样的问题。

任何提示?

提前致谢,

阿尔夫。

P:哦!顺便说一下,我很想知道做我做的事情做得好,从两个查询创建一个对象,把第一个值放在第二个查询中。谢谢!

1 个答案:

答案 0 :(得分:0)

解决了!我的问题不是代码,而是其他XML文件是空的...感谢您的帮助!