Linq to XML命名空间问题 [英] Linq to XML Namespace problems

查看:82
本文介绍了Linq to XML命名空间问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候,

我的目标:要验证XML文档,然后将数据加载到自定义对象中.我正在使用Linq到XML.

My goal: To validate an XML document then load the data into a custom object. I'm using Linq to XML.

我的处境:我在命名空间和/或Linq语法上苦苦挣扎.我以为一切都正常.该代码读取XML并加载该对象,但意识到XDocument.Validate正在传递所有内容,而没有真正进行验证.我想这是松懈的验证.为了使XDocument.Validate()方法有效,我在XML文件中添加了一个命名空间.验证有效,但是现在由我的Linq查询创建的XElement在尝试访问.Element("Field").value时返回null.

My situation: I'm struggling with the Namespace and/or the Linq syntax. I thought everything was working. The code reads the XML and loads the object, but realized the XDocument.Validate was passing everything through and not really validating. I guess that's lax validation. To get the XDocument.Validate() method to validate, I added a Namespace to the XML file. The Validation works, but now the XElement created by my Linq Query returns null when trying to access .Element("Field").value.

我的问题:

  1. 如何既可以验证XML文档又可以访问Elements的值?除了使用XDocument.Validate之外,是否还应该使用其他过程针对XSD验证XML?
  2. Linq查询是否有问题?
  3. 当我尝试在Linq查询中指定特定元素时,我从未得到结果.它 仅在适用时有效

  1. How can I both validate the XML document and access the value of the Elements? Should I use another process to validate the XML against an XSD besides using XDocument.Validate?
  2. Is the problem in my Linq query?
  3. When I try to specify a specific Element in the Linq query, I never get results. It only works when it is

IEnumerable<XElement> residents =
        from xeRes in xD.Elements()
        select xeRes;

但是

IEnumerable<XElement> residents =
        from xeRes in xD.Elements("Resident")
        select xeRes;

不返回任何内容.

任何建议都将受到欢迎.

Any suggestions would be most welcome.

谢谢

代码段:

XSD

<?xml version="1.0" encoding="utf-8"?>  <xsd:schema xmlns="http://kinduit.net/ResidentNS" xmlns:schema="http://kinduit.net/ResidentNS" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:sqltypes="http://schemas.microsoft.com/sqlserver/2004/sqltypes" elementFormDefault="qualified"><xsd:import namespace="http://schemas.microsoft.com/sqlserver/2004/sqltypes" schemaLocation="http://schemas.microsoft.com/sqlserver/2004/sqltypes/sqltypes.xsd" />
<xsd:element name="Resident">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="FacilityID">
                <xsd:simpleType>
                    <xsd:restriction base="sqltypes:varchar" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth" sqltypes:sqlSortId="52">
                        <xsd:maxLength value="50" />
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:element>
            <xsd:element name="ResidentID"  maxOccurs="unbounded">
                <xsd:simpleType>
                    <xsd:restriction base="sqltypes:varchar" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth" sqltypes:sqlSortId="52">
                        <xsd:maxLength value="20" />
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:element>
            <xsd:element name="ResidentID2"  maxOccurs="unbounded">
                <xsd:simpleType>
                    <xsd:restriction base="sqltypes:varchar" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth" sqltypes:sqlSortId="52">
                        <xsd:maxLength value="20" />
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:element>...

XML

<?xml version="1.0"?><Resident xmlns="http://kinduit.net/ResidentNS">
<FacilityID>WARMSPRINGS</FacilityID>
<ResidentID>WS585459</ResidentID>
<ResidentID2>145214</ResidentID2>...

C#

        // Validate XML Schema...
        XmlSchemaSet sc = new XmlSchemaSet();
        XNamespace xNs = "http://kinduit.net/ResidentNS";

        try
        {
            // Validate against the XSD...
            string location = System.Reflection.Assembly.GetAssembly(typeof(ElementsBC.Interface)).Location;
            sc.Add(xNs.ToString(), location.Replace("ElementsBC.dll", "") + "\\InterfaceXSD\\resident.xsd");
            XDocument xD = this.ConvertToXDocument(ResidentXML);
            xD.Validate(sc, (sender, e) => { throw new Exception(e.Message); }, true);

            IEnumerable<XElement> residents =
                from xeRes in xD.Elements()
                select xeRes;

            counts[0] = residents.Count();

            foreach (XElement el in residents)
            {
                try
                {
                    // get facility...
                    string facilityid = el.Element("FacilityID").Value.ToString();

如果XML中有一个命名空间,则最后一行返回null.如果没有命名空间,则此代码将运行宽松的Validation,但可以读取该值.

This last line returns null if there is a Namespace in the XML. If there is no Namespace, this code runs lax Validation, but can read the value.

建议?

推荐答案

您需要在调用中包括名称空间.

you need to include the namespace in the call.

XNamespace ns = xD.GetDefaultNamespace() // Or some other way of getting the namespace you want.
IEnumerable<XElement> residents = from xeRes in xD.Elements(ns + "Resident")        
                                  select xeRes;

这篇关于Linq to XML命名空间问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆