【问题标题】:Read element within elements using XmlTextReader使用 XmlTextReader 读取元素内的元素
【发布时间】:2015-07-15 18:15:44
【问题描述】:

我正在阅读XML data 并根据element 检索值。有一个名为<UniqueColumns> 的元素可以有一个名为<string> 的子元素。我想读取这些值并将其添加到ObservableCollection<String>。如果没有值,那么什么都不做。分三种情况:

场景 - 1:超过 1 个子元素。

<IndexId>4</IndexId>
<UniqueColumns>
  <string>Dir_nbr</string>
  <string>Dir_name</string>
</UniqueColumns>
<SelectedTableForUniqColumn>TBDirectory</SelectedTableForUniqColumn>

场景 - 2:只有一个子元素。

<IndexId>4</IndexId>
<UniqueColumns>
  <string>Dir_nbr</string>
</UniqueColumns>
<SelectedTableForUniqColumn>TBDirectory</SelectedTableForUniqColumn>

场景 - 3:没有子元素。

<IndexId>4</IndexId>
<UniqueColumns/>
<SelectedTableForUniqColumn>TBDirectory</SelectedTableForUniqColumn>

代码:

//This is a user defined data object and it has parameter which is type of `ObservableCollection<String>`. 
ExternalDatabaseTableRequestDO req = new ExternalDatabaseTableRequestDO();

using (XmlTextReader reader = new XmlTextReader(new StringReader(xmlData)))
{
    while (reader.Read())
    {
        int result;
        long res;
        string parameterValue;
        ObservableCollection<String> parameterValueList = new ObservableCollection<String>();

        switch (reader.Name.ToLower())
        {
            case "indexid":
                parameterValue = reader.ReadString();
                if (!String.IsNullOrWhiteSpace(parameterValue) && Int32.TryParse(parameterValue, out result))
                   req.IndexId = result;
                break;

            case "uniquecolumns":
                //need loop logic here but not sure how to do that.
                if (reader.NodeType == XmlNodeType.Element) // This will give me parent element which is <UniqueColumns>
                {
                    //Stuck here. How to read child elements if exists.
                }
                break;

            case "selectedtableforuniqcolumn":
                parameterValue = reader.ReadString();
                req.SelectedTableForUniqColumn = parameterValue;
                break;
        }
    }
}
return req;

【问题讨论】:

    标签: c# xmltextreader


    【解决方案1】:

    使用 Linq2Xml 怎么样?

    var xDoc = XDocument.Load(filename);
    //var xDoc = XDocument.Parse(xmlstring);
    var strings = xDoc.XPathSelectElements("//UniqueColumns/string")
                    .Select(x => x.Value)
                    .ToList();
    

    【讨论】:

    • 由于某种原因我收到错误'System.Xml.Linq.XDocument' does not contain a definition for 'XPathSelectElements'
    • 没关系,我修复了它,但又遇到了另一个错误Illegal characters in path.
    • @Robinhood 如果你给 XDocument 一个 xml 字符串 使用 XDocument.Parse 方法。 XDocument.Load 从文件中加载 xml。我更新了答案。
    • 感谢您的建议。我已经在下面发布了我的方法。
    【解决方案2】:
    //This will give me all child element values if they exists   
     var columnList = XElement.Parse(xmlData).Descendants("string").ToList();
    
            if (columnList != null)
            {
                foreach (var column in columnList)
                    parameterValueList.Add(column.Value);
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-01
      • 1970-01-01
      • 2013-10-06
      • 1970-01-01
      相关资源
      最近更新 更多