2015-11-24 93 views
0

我正在尋找從我稱爲initialInspections.xml的XML文件加載重複的元素。問題是系統需要動態,並允許添加許多不同的inspectionNotes。即使它們具有相同的名稱,我也需要輸入它們全部。從XML文件加載重複元素

如果有人可以請給我一個這樣做的方法,我會非常感激,因爲我一直在尋找近3個小時,我還沒有找到任何有效的工具。

我需要全部關閉每個inspectionNote節點內的數據,並將其放入一個名爲initialInspectionNotes的結構的數組中。

這裏是我到現在爲止:

public int propertyID; 
    public string initialInspectorUsername; 
    public DateTime intialDateTime; 
    public struct initialInspectionNotes 
    { 
     public string locationWithinProperty; 
     public string locationExtraNote; 
     public string costCode; 
     public float estimatedTime; 
    } 
    private void finalInspection_Load(object sender, EventArgs e) 
    { 
     //Open the intialInspections xml file and load the values into the form 
     XmlDocument xdoc = new XmlDocument(); 
     FileStream rFile = new FileStream(values.xmlInitialFileLocation, FileMode.Open); 
     xdoc.Load(rFile); 
     XmlNodeList list = xdoc.GetElementsByTagName("initialInspection"); 
     for (int i = 0; i < list.Count; i++) 
     { 
      XmlElement initialInspection = (XmlElement)xdoc.GetElementsByTagName("initialInspection")[i]; 
      XmlElement initialInspector = (XmlElement)xdoc.GetElementsByTagName("userInspection")[i]; 
      XmlElement dateTime = (XmlElement)xdoc.GetElementsByTagName("dateTime")[i]; 
      propertyID = int.Parse(initialInspection.GetAttribute("propertyID")); 
      initialInspectorUsername = initialInspector.InnerText; 
      intialDateTime = DateTime.Parse(dateTime.InnerText); 
     } 
     rFile.Close(); 
    } 

的XML看起來是這樣的:

<?xml version="1.0" standalone="yes"?> 
<initialInspections> 
    <initialInspection propertyID="1"> 
     <userInspection>defaultadmin</userInspection> 
     <dateTime>07/11/2015 17:15:20</dateTime> 
     <inspectionNote> 
      <location>Dining Room</location> 
      <locationNote>Remove whole carpet, leave underlay</locationNote> 
      <CostCode>L1</CostCode> 
      <estimatedTime>5</estimatedTime> 
     </inspectionNote> 
     <inspectionNote> 
      <location>Other - See Notes</location> 
      <locationNote>On the marked area with orange spray paint.</locationNote> 
      <CostCode>B1</CostCode> 
      <estimatedTime>12</estimatedTime> 
     </inspectionNote> 
    </initialInspection> 
</initialInspections> 

任何幫助將非常感激。

+0

去這個網站在這裏驗證XML文件。它有您需要首先解決這些XML錯誤的一些錯誤。 http://codebeautify.org/xmlvalidate你怎麼用這個'公共結構initialInspectionNotes'我個人會創建一個'類'然後我會創建一個'var lstNotes = new List ()'然後在循環填充字段,確保您使用循環頂部的新構造以及循環底部的列表的.Add方法將循環內部的lstNotes對象添加到不同的列表 MethodMan

+0

去編輯你的問題,爲什麼你應該改變你的XML以便它能夠基於'validate XML'免費在線工作 – MethodMan

+0

@MethodMan我剛剛通過該網站運行我的文件,並表示它沒問題。 – DanielWoodward

回答

1

一種可能是使用LINQ2XMLLINQ Descendants方法,一次擷取所有inspectionNotes

var xml = XDocument.Load(fileLocation); // for example c:\temp\input.xml 
// fetch all inspectionNotes 
var inspectionNotes = xml.Root.Descendants("inspectionNote").ToList(); 
// TODO: error handling! 
// map inspectionNote node to custom structure 
var arrayOfNotes = inspectionNotes.Select (n => new initialInspectionNotes 
{ 
    costCode = n.Element("CostCode").Value, 
    estimatedTime = float.Parse(n.Element("estimatedTime").Value), 
    locationExtraNote = n.Element("locationNote").Value, 
    locationWithinProperty = n.Element("location").Value, 
}) 
// and convert the result to array holding elements of the custom structure 
.ToArray(); 
foreach (var note in arrayOfNotes) 
{ 
    Console.WriteLine(note.locationExtraNote); 
} 

輸出是:

Remove whole carpet, leave underlay 
On the marked area with orange spray paint. 

邏輯同樣適用,如果你想讀和映射另一個XML節點(fe initialInspection)。


如果您需要使用XmlReader那麼爲了獲取內部inspectionNote元素和每一個inspectionNote元素的值,使用XmlNode.SelectSingleNodeXmlNode.SelectNodes使用XPath

//Open the intialInspections xml file and load the values into the form 
    XmlDocument xdoc = new XmlDocument(); 
    FileStream rFile = new FileStream(values.xmlInitialFileLocation, FileMode.Open); 
    xdoc.Load(rFile); 
    XmlNodeList list = xdoc.GetElementsByTagName("initialInspection"); 
    // create list of initialInspectionNotes in order to add as many nodes as needed 
    var notes = new List<initialInspectionNotes>(); 
    // map data 
    for (int i = 0; i < list.Count; i++) 
    { 
     // read data 
     XmlElement initialInspection = (XmlElement)xdoc.GetElementsByTagName("initialInspection")[i]; 
     XmlElement initialInspector = (XmlElement)xdoc.GetElementsByTagName("userInspection")[i]; 
     XmlElement dateTime = (XmlElement)xdoc.GetElementsByTagName("dateTime")[i]; 
     propertyID = int.Parse(initialInspection.GetAttribute("propertyID")); 
     initialInspectorUsername = initialInspector.InnerText; 
     intialDateTime = DateTime.Parse(dateTime.InnerText); 
     // fetch notes! 
     var inspectionNotes = initialInspection.SelectNodes("inspectionNote"); 
     foreach (XmlNode inspectionNote in inspectionNotes) 
     { 
      // insert data into list 
      notes.Add(new initialInspectionNotes 
           { 
            locationExtraNote = inspectionNote.SelectSingleNode("locationNote").InnerText, 
            costCode = inspectionNote.SelectSingleNode("CostCode").InnerText, 
            locationWithinProperty = inspectionNote.SelectSingleNode("location").InnerText 
           }); 
     } 
    } 
    // convert to array if needed 
    //var arrayOfNotes = notes.ToArray(); 
    rFile.Close(); 

不管有多少inspectionNote元素XML包含,列表和resp。數組將讀取它們全部。

+0

謝謝你,這正是我需要:) – DanielWoodward

+0

很高興答案幫助。 – pasty

+0

好吧,所以我似乎實際上遇到錯誤,一旦它到達notes.Add行。 System.ArgumentNullException它似乎沒有實際的值被加載的代碼,它只是錯誤時,它擊中該行。 – DanielWoodward

3
class Note 
{ 
    public string Location { get; set; } 
    public string LocationNote { get; set; } 
    public string CodeCost { get; set; } 
    public string EstimatedTime { get; set; } 
} 

var xml = XElement.Load(...your xml path here); 

var data = xml.Descendants("initialInspection").Elements("inspectionNote").Select(n => new  Note() 
{ 
    Location = n.Element("location").Value, 
    LocationNote = n.Element("locationNote").Value, 
    CodeCost = n.Element("CostCode").Value, 
    EstimatedTime = n.Element("estimatedTime").Value 

}).ToList(); 
0
class InitialInspectionNotes 
{ 
    public string Location { get; set; } 
    public string LocationNote { get; set; } 
    public string CodeCost { get; set; } 
    public string EstimatedTime { get; set; } 
} 

var xdoc = XDocument.Load("yourpath\filename.xml"); 

var dataXml = xdoc.Descendants("initialInspection").Elements("inspectionNote").Select(n => new InitialInspectionNotes() 
{ 
    Location = n.Element("location").Value, 
    LocationNote = n.Element("locationNote").Value, 
    CodeCost = n.Element("CostCode").Value, 
    EstimatedTime = n.Element("estimatedTime").Value 

}).ToList(); 

var xmlList = new List<object>(); 
for (int i = 0; i < dataXml.Count; i++) 
{ 
    xmlList.Add(dataXml[i]); 
}