2

我有一个 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是静态数据时将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

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

4

2 回答 2

2

就像是:

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; }
}
于 2013-09-05T08:59:43.160 回答
0

您的代码几乎是正确的:

  1. XML 区分大小写,因此

    str.Elements("DATA")
    

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

    str.Elements("data")
    
  2. result将是 a ,当你使用它的方法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得到结果就挂断电话,把a转IEnumerable<XElement>成aList<XElement>只是消耗CPU周期和内存,没有任何好处。

于 2013-09-05T09:03:00.387 回答