2010-12-30 54 views
15

Tôi có ba Danh sách trong C#, tên biến là l_lstData1, l_lstData2, l_lstData3.LINQ và XDocument: Cách tạo tệp XML?

cấu trúc tập tin là

<FileDetails> 
    <Date FileModified="29/04/2010 12:34:02" /> 
    <Data Name="Data_1" DataList="India" Level="2" /> 
    <Data Name="Data_2" DataList="chennai" Level="2" /> 
    <Data Name="Data_3" DataList="hyderabad" Level="2" /> 
    <Data Name="Data_4" DataList="calcutta" Level="2" /> 
    <Data Name="Data_5" DataList="vijayawada" Level="1" /> 
    <Data Name="Data_6" DataList="cochin" Level="1" /> 
    <Data Name="Data_7" DataList="madurai" Level="0" /> 
    <Data Name="Data_8" DataList="trichy" Level="0" /> 
</FileDetails> 

các giá trị của 3 Chức năng như sau:

l_lstData1[0] = "India"; 
l_lstData1[1] = "chennai"; 
l_lstData1[2] = "hyderabad"; 
l_lstData1[3] = "calcutta"; 

nên thuộc tính cấp độ của các XML ở trên (phần tử: dữ liệu) có value = "2".

l_lstData2[0] = "vijayawada"; 
l_lstData2[1] = "cochin";  

để thuộc tính mức của XML trên (phần tử: Dữ liệu) có giá trị = "1".

l_lstData3[0] = "madurai"; 
l_lstData3[1] = "trichy";  

để thuộc tính mức của XML trên (phần tử: Dữ liệu) có giá trị = "0".

Trả lời

30

Nó không rõ ràng chính xác tại sao "Cấp" thuộc tính được theo quy định, nhưng điều này sẽ tạo ra XML thích hợp với bạn:

// Used for side-effects in the XElement constructor. This is a bit icky. It's 
// not clear what the "Name" part is really for... 
int count = 1; 

var doc = new XDocument(
    new XElement("FileDetails", 
     new XElement("Date", new XAttribute("FileModified", DateTime.UtcNow)), 
     l_lstData1.Select(x => new XElement("Data", 
      new XAttribute("Name", "Data_" + count++), 
      new XAttribute("DataList", x), 
      new XAttribute("Level", 2))), 
     l_lstData2.Select(x => new XElement("Data", 
      new XAttribute("Name", "Data_" + count++), 
      new XAttribute("DataList", x), 
      new XAttribute("Level", 1))), 
     l_lstData3.Select(x => new XElement("Data", 
      new XAttribute("Name", "Data_" + count++), 
      new XAttribute("DataList", x), 
      new XAttribute("Level", 0))))); 

Nó có lẽ sẽ gọn gàng hơn nếu bạn có thể trích xuất các dự báo từ một mục danh sách cho phần tử của nó, nhưng bit "Data_" + count làm cho việc này trở nên phức tạp. Nó không rõ ràng tại sao bạn cần một điều như vậy để được trung thực ... nếu bạn có thể nhận được đi mà không có điều đó, mã có thể được sạch hơn.

Tôi giả sử một giải pháp thay thế là tạo tài liệu mà không cần thuộc tính Name và sau đó điền chúng sau đó. Ví dụ:

private static IEnumerable<XElement> ProjectList(IEnumerable<string> list, 
    int level) 
{ 
    return list.Select(x => new XElement("Data", 
     new XAttribute("DataList", x), 
     new XAttribute("Level", level))); 
} 

thì:

var doc = new XDocument(
    new XElement("FileDetails", 
     new XElement("Date", new XAttribute("FileModified", DateTime.UtcNow)), 
     ProjectList(l_lstData1, 2), 
     ProjectList(l_lstData2, 1), 
     ProjectList(l_lstData3, 0))); 

int count = 1; 
foreach (var element in doc.Descendants("Data")) 
{ 
    element.SetAttributeValue("Name", "Data_" + count++); 
} 
2

gì về:

XDocument doc = new XDocument(); 

var total = (from a in list1 select new { Name = a, Level = 2 }).Concat(
      from b in list2 select new { Name = b, Level = 1 }).Concat(
      from c in list3 select new { Name = c, Level = 0 }); 

XElement root = new XElement("FileDetails", from i in Enumerable.Range(0, total.Count()) 
              let element = total.ElementAt(i) 
              let name = new XAttribute("Name", String.Format("Data_{0}", i + 1)) 
              let level = new XAttribute("Level", element.Level) 
              let datalist = new XAttribute("DataList", element.Name) 
              select new XElement("Data", name, datalist, level), 
              new XElement("Date", new XAttribute("FileModified", DateTime.Now))); 
Các vấn đề liên quan