LINQ to XML 不返回默认值

发布于 2024-10-19 13:16:28 字数 450 浏览 3 评论 0原文

我在 XSD 中指定了一个默认值,如下所示:

<xs:complexType name="image">
    <xs:attribute name="path" type="xs:string" />
    <xs:attribute name="type" type="imageType" default="normal" />
</xs:complexType>

但是,除非我在 XML 中显式包含一个值,否则当我运行如下所示的 LINQ 查询时,“类型”始终作为空字符串返回:

Dim images = From i In collection.<image> Select i.@path, i.@type

这是预期的结果还是可能的情况省略该属性并让 LINQ 检查默认值?

I have a default value specified in XSD as follows:

<xs:complexType name="image">
    <xs:attribute name="path" type="xs:string" />
    <xs:attribute name="type" type="imageType" default="normal" />
</xs:complexType>

But unless I explicitly include a value in the XML, 'type' is always returned as an empty string when I run a LINQ query such as this:

Dim images = From i In collection.<image> Select i.@path, i.@type

Should this be expected or is it possible to omit the attribute and let LINQ check for a default value?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那么,您是否首先根据您的架构验证输入 XML?
下面是一个示例:

Dim xrs As New XmlReaderSettings()
xrs.Schemas.Add(Nothing, "..\..\XMLSchema1.xsd")
xrs.ValidationType = ValidationType.Schema

Dim doc As XDocument

Using xr As XmlReader = XmlReader.Create("..\..\XMLFile1.xml", xrs)
    doc = XDocument.Load(xr)
End Using

For Each img As XElement In doc.<images>.<image>
    Console.WriteLine("Type: {0}", img.@type)
Next

模式为

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="images">
    <xs:complexType>
      <xs:sequence maxOccurs="unbounded">
        <xs:element name="image">
          <xs:complexType>
            <xs:attribute name="type" type="xs:string" default="normal"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

,XML 输入为

<images>
  <image/>
  <image type="vector"/> 
</images>

示例输出

Type: normal
Type: vector

Well do you first validate the input XML against your schema?
Here is an example:

Dim xrs As New XmlReaderSettings()
xrs.Schemas.Add(Nothing, "..\..\XMLSchema1.xsd")
xrs.ValidationType = ValidationType.Schema

Dim doc As XDocument

Using xr As XmlReader = XmlReader.Create("..\..\XMLFile1.xml", xrs)
    doc = XDocument.Load(xr)
End Using

For Each img As XElement In doc.<images>.<image>
    Console.WriteLine("Type: {0}", img.@type)
Next

With the schema being

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="images">
    <xs:complexType>
      <xs:sequence maxOccurs="unbounded">
        <xs:element name="image">
          <xs:complexType>
            <xs:attribute name="type" type="xs:string" default="normal"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

and the XML input being

<images>
  <image/>
  <image type="vector"/> 
</images>

that sample outputs

Type: normal
Type: vector
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文