无法使用XElement检索XML元素的值

时间:2013-07-15 12:03:02

标签: c# xml-parsing linq-to-xml xelement

我正在尝试从通过文件浏览器提供的XML文件中读取,但是即使我可以看到xml文件通过元素,值仍为null。

  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>

在普通文件中,文件中会有数据,显然我只需要为样本删除它。

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

4 个答案:

答案 0 :(得分:2)

您需要指定(默认)命名空间。

  var xdoc = XElement.Load(fileStream);
  var dns = xdoc.GetDefaultNamespace();
  ....

然后你的循环变为(添加了一些范围校正)

  foreach (var item in elements)
  {
      var codeList = new CodeList();

      codeList.Name = item.Element(dns + "CODELIST_NAME").Value;
      codeList.Description = item.Element(dns + "DESRIPTION").Value;
      codeList.Version = item.Element(dns + "VERSION").Value;
      codeList.EffectiveDate = DateTime.Parse(item.Element(dns + "EFFECTIVE_DATE").Value);
      codeList.ExpirationDate = DateTime.Parse(item.Element(dns + "EXPIRATION_DATE").Value);

      // save code list
  }

答案 1 :(得分:1)

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

定义您要解析的元素的父元素。例如,如果您的xml如下:

<parent>
    <CODELIST_NAME></CODELIST_NAME>
    <DESRIPTION></DESRIPTION>
    <VERSION></VERSION>
    <EFFECTIVE_DATE></EFFECTIVE_DATE>
    <EXPIRATION_DATE></EXPIRATION_DATE>
</parent>

然后将其写为

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

Giannis

答案 2 :(得分:1)

我知道你找到了答案,但是我想把我所做的让它正常运行只是为了好玩:)这里是我使用的代码:

using System;
using System.Xml;
using System.Xml.Linq;

namespace xmlTesting
{
  public class codeList
  {
    public string Name { get; set; }
    public string Description { get; set; }
    public string Version { get; set; }
    public DateTime EffectiveDate { get; set; }
    public DateTime ExpirationDate { get; set; }
  }

  class program {

    static void Main(string[] args)
    {
      var CL = UploadXml(XElement.Load(@"c:\debug\xmlcontent.xml"));
      Console.WriteLine(
        string.Format("name: {0}\nDesc: {1}\nVersion: {2}\nEffectivdate: {3}\nExp: {4}"
        , CL.Name, CL.Description, CL.Version, CL.EffectiveDate, CL.ExpirationDate)
        );
      Console.ReadKey(true);
    }

    public static codeList UploadXml(XElement xdoc)
    {
      var codeList = new codeList();

      foreach (XElement XE in xdoc.Descendants())
      {
        switch (XE.Name.LocalName)
        {
          case "CODELIST_NAME":
            codeList.Name = XE.Value;
            break;
          case "DESCRIPTION":
            if(codeList.Description == null)
            codeList.Description = XE.Value;
            break;
          case "VERSION":
            codeList.Version = XE.Value;
            break;
          case "EFFECTIVE_DATE":
            codeList.EffectiveDate = DateTime.Parse(XE.Value);
            break;
          case "EXPIRATION_DATE":
            codeList.ExpirationDate = DateTime.Parse( XE.Value);
            break;
        }
      }
      // save code list

      // get code list ID

      // create codes
      return codeList;
    }
  }
}

我使用的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>a name</CODELIST_NAME>
        <DESCRIPTION>a desr</DESCRIPTION>
        <VERSION>test1.1</VERSION>
        <USERNAME>test user</USERNAME>
        <OWNER_TEAM />
        <STEWARD_TEAM />
        <STATUS></STATUS>
        <LAST_MODIFIED>a day</LAST_MODIFIED>
        <LAST_MODIFIED_NAME>trae</LAST_MODIFIED_NAME>
        <EFFECTIVE_DATE>07/05/1983</EFFECTIVE_DATE>
        <EXPIRATION_DATE>07/05/1983</EXPIRATION_DATE>
        <FILE_TIMESTAMP></FILE_TIMESTAMP>
       <CONSTRAINED_VALUE>
           <CODE></CODE>
           <PARENT_ID />
           <NAME></NAME>
           <DESCRIPTION></DESCRIPTION>
       </CONSTRAINED_VALUE>
 </CODELIST>
</CONTEXT>

答案 3 :(得分:0)

感谢您的帮助,我可以解决此问题

我的问题在于我如何阅读这些价值观。

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

以这种方式读它已经为我修好了。