【问题标题】:XMLReader ReadToFollowing returns NoneXMLReader ReadToFollowing 返回无
【发布时间】:2021-07-22 20:21:55
【问题描述】:

这是 XML

     <GeoLoc>
      <Location>
          <AddrLn1>384-393 COMMERCIAL ROAD</AddrLn1>
          <Town>LONDON</Town>
          <PostCode>E1 0LR</PostCode>
          <Country>ENGLAND</Country>
          <UPRN>100080712803</UPRN>
        </Location>
      </GeoLoc>

这是我的子程序

private GeoLoc GetGeoLoc(XmlReader reader)
    {
        GeoLoc _geoloc = new GeoLoc();
        _geoloc.Location = new Location();
        


        if (reader.ReadToDescendant("Location"))
        {
            if (reader.ReadToFollowing("AddrLn1"))
            {
                _geoloc.Location.AddrLn1 = reader.ReadElementContentAsString();
            }
            if (reader.ReadToFollowing("AddrLn2"))
            {
                _geoloc.Location.AddrLn2 = reader.ReadElementContentAsString();
            }
            if (reader.ReadToFollowing("AddrLn3"))
            {
                _geoloc.Location.AddrLn3 = reader.ReadElementContentAsString();
            }
            if (reader.ReadToFollowing("Town"))
            {
                _geoloc.Location.Town = reader.ReadElementContentAsString();
            }
            if (reader.ReadToFollowing("County"))
            {
                _geoloc.Location.County = reader.ReadElementContentAsString();
            }
            if (reader.ReadToFollowing("PostCode"))
            {
                _geoloc.Location.PostCode = reader.ReadElementContentAsString();
            }
            if (reader.ReadToFollowing("Country"))
            {
                _geoloc.Location.Country = reader.ReadElementContentAsString();
            }
            if (reader.ReadToFollowing("UPRN"))
            {
                _geoloc.Location.UPRN = reader.ReadElementContentAsString();
            }
        }
        return _geoloc;
    }

问题是它只读取 AddrLn1,然后读取器变为无。我尝试了 ReadSubTree() 但结果相同。请注意,某些 Geoloc 示例中可能缺少某些标签,AddrLn2 或 AddrLn3 可能存在。

【问题讨论】:

  • 问题是没有地址行 2,所以您将在文件末尾找不到任何东西。您可以使用以下内容:XmlReader child = reader.ReadSubtree();
  • 谢谢@jdweng,我应该在哪里添加这个?以及如何检查以跳过丢失的元素?
  • 如果找不到项目,阅读器将处于 EOF。或者你可以移动到下一个元素。
  • 您需要两个阅读器,因此当您没有找到某个项目时,您仍然在当前位置。 XmlReader child = reader.ReadSubtree(); if (child.ReadToFollowing("Country")) { reader = child; _geoloc.Location.County = reader.ReadElementContentAsString();}
  • 所以我试过了,但是当 inner.ReadElementContentAsString();读取器和子读取器都被执行成为“空白,值=\”\“”

标签: c# xml xmlreader


【解决方案1】:

我通常将 XML Linq 与 XmlReader 一起使用。如果没有找到,你会得到一个空字符串。

           if (reader.ReadToDescendant("Location"))
            {
                XElement location = (XElement)XElement.ReadFrom(reader);

                string addrLn1 = (string)location.Element("AddrLn1");
                string addrLn2 = (string)location.Element("AddrLn2");
                string addrLn3 = (string)location.Element("AddrLn3");

            }

【讨论】:

    【解决方案2】:

    更新:最初下面的代码对我有用,但我找到了更好的解决方案

    private GeoLoc GetGeoLoc(XmlReader reader)
    {
        GeoLoc _geoloc = new GeoLoc();
        _geoloc.Location = new Location();
    
        if (reader.ReadToDescendant("Location"))
        {
            do
            {
                reader.Read();
                reader.MoveToContent();
                switch (reader.LocalName)
                {
                    case "AddrLn1":
                        _geoloc.Location.AddrLn1 = reader.ReadElementContentAsString();
                        break;
                    case "AddrLn2":
                        _geoloc.Location.AddrLn2 = reader.ReadElementContentAsString();
                        break;
                    case "AddrLn3":
                        _geoloc.Location.AddrLn3 = reader.ReadElementContentAsString();
                        break;
                    case "Town":
                        _geoloc.Location.Town = reader.ReadElementContentAsString();
                        break;
                    case "County":
                        _geoloc.Location.County = reader.ReadElementContentAsString();
                        break;
                    case "PostCode":
                        _geoloc.Location.PostCode = reader.ReadElementContentAsString();
                        break;
                    case "Country":
                        _geoloc.Location.Country = reader.ReadElementContentAsString();
                        break;
                    case "UPRN":
                        _geoloc.Location.Country = reader.ReadElementContentAsString();
                        break;
                }
    
            } while (reader.NodeType != XmlNodeType.None);
            
        }
        return _geoloc;
    }
    

    这是新代码,我决定使用 XElement,而不是使用阅读器。性能是一样的。

    private GeoLoc GetGeoLoc(XElement element)
    {
        GeoLoc _geoloc = new GeoLoc();
        _geoloc.Location = new Location();
        foreach (XElement geolocel in element.Elements())
        {
            foreach (XElement locationel in geolocel.Elements())
            {
                switch (locationel.Name.LocalName)
                {
                    case "AddrLn1":
                        _geoloc.Location.AddrLn1 = locationel.Value;
                        break;
                    case "AddrLn2":
                        _geoloc.Location.AddrLn2 = locationel.Value;
                        break;
                    case "AddrLn3":
                        _geoloc.Location.AddrLn3 = locationel.Value;
                        break;
                    case "Town":
                        _geoloc.Location.Town = locationel.Value;
                        break;
                    case "County":
                        _geoloc.Location.County = locationel.Value;
                        break;
                    case "PostCode":
                        _geoloc.Location.PostCode = locationel.Value;
                        break;
                    case "Country":
                        _geoloc.Location.Country = locationel.Value;
                        break;
                    case "UPRN":
                        _geoloc.Location.Country = locationel.Value;
                        break;
                }
            }
        }
        return _geoloc;
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-23
      • 2014-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多