无法使用XElement检索XML元素的值 [英] Can't Retrieve Values of XML Elements using XElement

查看:94
本文介绍了无法使用XElement检索XML元素的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图读取通过文件浏览器提供的XML文件,但是即使我可以看到xml文件都通过了元素,这些值也始终为空.

I am trying to read from an XML file that is supplied through a file browser but the values are alwas null even though I can see the xml file come through to elements.

  public void UploadXml(Stream fileStream)
  {
    //Load xml
      fileStream.Position = 0;

      var xdoc = XElement.Load(fileStream);

      IEnumerable<XElement> elements = xdoc.Elements();

      var codeList = new CodeList();

      foreach (var item in elements)
      {
          codeList.Name = item.Element("CODELIST_NAME").Value;
          codeList.Description = item.Element("DESRIPTION").Value;
          codeList.Version = item.Element("VERSION").Value;
          codeList.EffectiveDate = DateTime.Parse(item.Element("EFFECTIVE_DATE").Value);
          codeList.ExpirationDate = DateTime.Parse(item.Element("EXPIRATION_DATE").Value);
      }
      // save code list

      // get code list ID

      // create codes
  }

更新XML

<?xml version="1.0" encoding="utf-8"?>
<CONTEXT xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns:xsd="http://www.w3.org/2001/XMLSchema"    
         xmlns="http://www.w3.org/2000/xmlns">
  <CONTEXT_NAME></CONTEXT_NAME>
  <CODELIST>
    <CODELIST_NAME></CODELIST_NAME>
        <DESRIPTION></DESRIPTION>
        <VERSION></VERSION>
        <USERNAME>test user</USERNAME>
        <OWNER_TEAM />
        <STEWARD_TEAM />
        <STATUS></STATUS>
        <LAST_MODIFIED></LAST_MODIFIED>
        <LAST_MODIFIED_NAME></LAST_MODIFIED_NAME>
        <EFFECTIVE_DATE></EFFECTIVE_DATE>
        <EXPIRATION_DATE></EXPIRATION_DATE>
        <FILE_TIMESTAMP></FILE_TIMESTAMP>
       <CONSTRAINED_VALUE>
           <CODE></CODE>
           <PARENT_ID />
           <NAME></NAME>
           <DESCRIPTION></DESCRIPTION>
       </CONSTRAINED_VALUE>
 </CODELIST>
</CONTEXT>

在普通文件中,文件中显然会有数据,我只需要删除样本中的数据即可.

In a normal file there would be data in the file obviously I just had to remove it for the sample.

约束值标签将针对数据库中的行重复

The constrained value tags will be repeated for rows in a database

推荐答案

感谢您的帮助,对我来说解决此问题非常有用

Thanks for the help, it was useful for me to fix this

我的问题在于我如何读取值.

My problem was with how I was reading the values.

xdoc.Element(XName.Get("CODELIST", dns.NamespaceName)).Element(XName.Get("CODELIST_NAME", dns.NamespaceName)).Value,

以这种方式阅读已为我修复.

Reading it this way has fixed it for me.

这篇关于无法使用XElement检索XML元素的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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