存储XML中的元素

本文关键字:元素 XML 存储 | 更新日期: 2023-09-27 18:30:12

我有一个XML文件::

<data>
    <find>TAG11</find>
    <data_type>Static Data</data_type>
    <static_value>Varun</static_value>
    <field_name>Not Applicable</field_name>
    <table_name>Batch_Master</table_name>
</data>
<data>
    <find>VAL1</find>
    <data_type>Dynamic Data</data_type>
    <static_value>Not Applicable</static_value>
    <field_name>Product Name</field_name>
    <table_name>Batch_Master</table_name>
</data>
<data>
    <find>TAG1</find>
    <data_type>Dynamic Data</data_type>
    <static_value>Not Applicable</static_value>
    <field_name>Country Name</field_name>
    <table_name>Batch_Master</table_name>
</data>
<data>
    <find>VAL11</find>
    <data_type>Static Data</data_type>
    <static_value>Nayak</static_value>
    <field_name>Not Applicable</field_name>
    <table_name>Batch_Master</table_name>
</data>
<data>
    <find>VAL2</find>
    <data_type>Dynamic Data</data_type>
    <static_value>Not Applicable</static_value>
    <field_name>Batch Code</field_name>
    <table_name>Batch_Master</table_name>
</data>
<data>
    <find>TAG12</find>
    <data_type>Dynamic Data</data_type>
    <static_value>Not Applicable</static_value>
    <field_name>Carton Number</field_name>
    <table_name>Batch_Master</table_name>
</data>
<data>
    <find>VAL3</find>
    <data_type>Dynamic Data</data_type>
    <static_value>Not Applicable</static_value>
    <field_name>Product Strength</field_name>
    <table_name>Batch_Master</table_name>
</data>
<data>
    <find>TAG10</find>
    <data_type>Not Selected</data_type>
    <static_value>Not Applicable</static_value>
    <field_name>Not Selected</field_name>
    <table_name>Batch_Master</table_name>
</data>

现在,只有当data_type为StaticData,时,我才需要将find和static_value值存储在字符串中

我对XMLtoLinq不太了解,但我写了这个函数

var xmlStr = File.ReadAllText(@"D:'Test'DATA020913.xml");
var str = XElement.Parse(xmlStr);
var result = str.Elements("DATA")
    .Where(x => x.Element("Data_Type").Value.Equals("Static Data")).ToList();
MessageBox.Show(result.ToString());   
Console.WriteLine(result);

通过这个,我得到了静态数据来的次数,bt我还希望变量中的find和static_value

仅供参考::稍后我将替换文本文件中的这些变量值。

存储XML中的元素

类似于:

var doc = XDocument.Parse(xmlStr);
var data = (from el in doc.Root.Elements("data")
            where (string)el.Element("data_type") == "Static Data"
            select new
            {
                Find = (string)el.Element("find"),
                StaticValue = (string)el.Element("static_value")
            }).ToList();
foreach (var el in data)
{
    Console.WriteLine("{0}, {1}", el.Find, el.StaticValue);
}

输出:

TAG11, Varun
VAL11, Nayak

或者,考虑使用XmlSerializer将数据反序列化为对象模型。例如:

SomeRootElement root;
using (var sr = new StringReader(xmlStr))
using (var xr = XmlReader.Create(sr))
{
    var ser = new XmlSerializer(typeof(SomeRootElement));
    root = (SomeRootElement)ser.Deserialize(xr);
}
var items = from item in root.Items
            where item.DataType == DataType.StaticData
            select item;
foreach (var item in items)
{
    Console.WriteLine("{0}, {1}", item.Find, item.StaticValue);
}

使用型号:

public enum DataType
{
    [XmlEnum("Static Data")]
    StaticData,
    [XmlEnum("Dynamic Data")]
    DynamicData,
    [XmlEnum("Not Selected")]
    NotSelected
}
[XmlRoot("xml")]
public class SomeRootElement
{
    private readonly List<DataItem> items = new List<DataItem>();
    [XmlElement("data")]
    public List<DataItem> Items { get { return items; } }
}
public class DataItem
{
    [XmlElement("find")]
    public string Find { get; set; }
    [XmlElement("data_type")]
    public DataType DataType { get; set; }
    [XmlElement("static_value")]
    public string StaticValue { get; set; }
    [XmlElement("field_name")]
    public string FieldName { get; set; }
    [XmlElement("table_name")]
    public string TableName { get; set; }
}

您的代码几乎是正确的:

  1. XML区分大小写,因此

    str.Elements("DATA")
    

    找不到任何东西,你需要:

    str.Elements("data")
    
  2. result将是List<XElement>,当您使用它的ToString方法时,它不会给您任何有用的东西。相反,您需要遍历列表,依次从每个XElement中提取信息:

    foreach (var el in result) {
      var findValue = el.Element("find").Value;
      var staticValue = el.Element("static_value");
      // Do something with the data.
    }
    

建议:在得到结果时放弃ToList调用,将IEnumerable<XElement>转换为List<XElement>只会消耗CPU周期和内存,没有任何好处。

相关文章: