首页 \ 问答 \ 使用LINQ选择是否存在XML元素(Select if exist XML Element using LINQ)

使用LINQ选择是否存在XML元素(Select if exist XML Element using LINQ)

我有以下xml。 由于addr在这种情况下是空的,但大部分时间它都有像街道,城市,州等元素。 我如何确保它存在。 现在的原因LINQ查询引发错误“对象引用未设置为对象的实例”。

  <author>
    <time value="20120620153233"/>
    <assignedAuthor>
      <id/>
      <addr/>
      <telecom value="tel:+1-781-271-3000"/>
      <assignedPerson>
        <name>Auto Generated</name>
      </assignedPerson>
    </assignedAuthor>
  </author>



 tbAuthor autor =
                    (from c in cdafile.Root.Elements(ns + "author")
                    select new tbAuthor
                    {                           
                        streetAddressLine = (string)c.Element(ns + "assignedAuthor").Element(ns + "addr").Element(ns + "streetAddressLine").Value,
                        city = (string)c.Element(ns + "assignedAuthor").Element(ns + "addr").Element(ns + "city").Value,
                        state = (string)c.Element(ns + "assignedAuthor").Element(ns + "addr").Element(ns + "state").Value,
                        postalCode = (string)c.Element(ns + "assignedAuthor").Element(ns + "addr").Element(ns + "postalCode").Value,
                        country = "US"
                    }).FirstOrDefault();

I have following xml . As addr is empty in this case but most time its has elements like street,city,state . How do I make sure it exist . Cause for now LINQ query throws an error "Object reference not set to an instance of an object."

  <author>
    <time value="20120620153233"/>
    <assignedAuthor>
      <id/>
      <addr/>
      <telecom value="tel:+1-781-271-3000"/>
      <assignedPerson>
        <name>Auto Generated</name>
      </assignedPerson>
    </assignedAuthor>
  </author>



 tbAuthor autor =
                    (from c in cdafile.Root.Elements(ns + "author")
                    select new tbAuthor
                    {                           
                        streetAddressLine = (string)c.Element(ns + "assignedAuthor").Element(ns + "addr").Element(ns + "streetAddressLine").Value,
                        city = (string)c.Element(ns + "assignedAuthor").Element(ns + "addr").Element(ns + "city").Value,
                        state = (string)c.Element(ns + "assignedAuthor").Element(ns + "addr").Element(ns + "state").Value,
                        postalCode = (string)c.Element(ns + "assignedAuthor").Element(ns + "addr").Element(ns + "postalCode").Value,
                        country = "US"
                    }).FirstOrDefault();

原文:https://stackoverflow.com/questions/17503614
更新时间:2023-04-11 16:04

最满意答案

您必须在方法名称displayTower和代码行中更改此代码 -

[array1 addObject:array];
 NSLog(@"Lat/Long %@ / %@",  lat1, long1);

与 -

[array1 addObject:array];
 NSLog(@"Lat/Long %@ / %@",  lat1, long1);
[[self pickerview1] reloadAllComponents];

可能会帮助你,或者感到自由。


You have to changed this code in the method name displayTower and code lines -

[array1 addObject:array];
 NSLog(@"Lat/Long %@ / %@",  lat1, long1);

with -

[array1 addObject:array];
 NSLog(@"Lat/Long %@ / %@",  lat1, long1);
[[self pickerview1] reloadAllComponents];

may be it will help you, or feel free.

相关问答

更多
  • 我终于通过使用sqlite3_step函数解决了这个问题: sqlite3_step(statement); I finally solved this by using the sqlite3_step function: sqlite3_step(statement);
  • 假设count很大,那个循环真的会减慢速度。 您可以使用executemany()来加快速度。 尝试用这个替换你的循环: params = end_of_day.apply(tuple).tolist() # Convert dataframe to list of tuples c.executemany('INSERT INTO MMM(open,high,low,close,volume,date) VALUES(?,?,?,?,?,?)', params) db.commit() 有关execu ...
  • 查看文档,我最好的猜测是,这是因为数据库文件在Vagrant的NFS挂载上。 根据文件: 需要注意的是,POSIX顾问锁定在许多NFS实现中都是错误的甚至是未实现的...最好的防御是不要在网络文件系统上使用SQLite。 https://www.sqlite.org/lockingv3.html 我能够通过在主机上的已安装文件夹上设置文件权限来解决此问题。 Looking at the docs, my best guess is that this is occuring because the data ...
  • 无法将新创建的数据库与先前创建的空数据库区分开来。 但是,空数据库是唯一存在此问题的数据库。 可以通过检查sqlite_master表是否为空来检测任何其他数据库。 在事务中执行此操作并创建表,并且没有竞争条件。 如果您第一次写入数据库(实际创建文件时)是设置application_id ,那么您知道任何具有其他ID的文件不是您的。 (已注册的应用程序ID是唯一的。) It is not possible to differentiate a newly-created database from an e ...
  • 根据事务开始后的SQLite3锁定引用 (BEGIN命令),在执行第一个SELECT语句时将获取SHARED锁。 共享锁意味着可以读取数据库但不能写入数据库。 执行第一个INSERT,UPDATE或DELETE语句时将获取RESERVED锁。 According to SQLite3 locking reference after start of transaction (BEGIN command), a SHARED lock will be acquired when the first SELEC ...
  • sqlite3不会自动存储更改日期,因此如果您想稍后获取更改日期,则应自行将日期插入数据库。 sqlite3 does not store change dates automatically, so if you want to get them later you should insert the date to database yourself.
  • 尝试在数据库配置中添加完整路径,这就是我在我的应用中所做的: 'Database/Sqlite', 'persistent' => false, 'host' => '', 'database' => D ...
  • 您必须在方法名称displayTower和代码行中更改此代码 - [array1 addObject:array]; NSLog(@"Lat/Long %@ / %@", lat1, long1); 与 - [array1 addObject:array]; NSLog(@"Lat/Long %@ / %@", lat1, long1); [[self pickerview1] reloadAllComponents]; 可能会帮助你,或者感到自由。 You have to changed th ...
  • [我想知道]如果通常的做法是拥有一个专用的SQLite3数据库PER CLIENT 仅当数据库与应用程序一起部署时,例如在电话上。 否则我从来没有听说过这样的事情。 我从来没有使用SQLite,我不知道更新模式和复制所有数据库中的更改的过程是否与其他RDBMS相同 SQLite是一个SQL数据库,它响应ALTER TABLE等。 至于更新所有模式,您必须重新运行所有模式的更新。 模式同步通常由外部实用程序处理,通常您的ORM会有一些东西。 有些是服务器不可知的,有些只支持特定的服务器。 还有专门的数据库变更 ...
  • 显式锁定数据库的方法是按照文档中的说明启动事务: 当数据库被多个连接访问时,其中一个进程修改了数据库,SQLite数据库被锁定,直到该事务被提交。 启动事务的一种方式是使用连接作为上下文管理器 : import sqlite3 con = sqlite3.connect(...) ... with con: # Database is locked here 另请注意,某些事务默认情况下会发生: 默认情况下,sqlite3模块在数据修改语言(DML)语句(即INSERT / UPDATE / DE ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)