2016-05-11 98 views
0

我試圖從WPF創建一個工具,該工具通過將數據輸入到文本框中創建一個簡單的XML文檔,然後單擊一個按鈕將該數據輸入到XML文檔中。除了根元素,我將有一個孩子分步驟元素的腳步元素像這樣:C#,試圖將子節點添加到具有特定屬性的節點

<root> 
<step id="1"> 
    <P>some instructions</p> 
    <step id="1.1"> 
     <p>some additional instructions</p> 
    </step> 
</step> 
<step id="2"> 
    <p>second set of instructions</p> 
    <step id="2.1"> 
     <p>additional instructions for step 2</p> 
    </step> 
</step> 

我已經能夠添加父步驟,但是我所有的子步驟,屬於第一父下步驟:

<root> 
<step id="1"> 
    <step id="1.1"> 
    <step id="2.1"> 
    <step id="3.1"> 
<step id="2"> 
<step id="3"> 

我想使用XPath插入我的子步驟到適當的父步驟。但是,我收到錯誤: 「XAttribute不包含'add'的定義,並且沒有找到接受第一個參數類型'XAttribute'的擴展方法'add'」。

我已經廣泛搜索,無法提出解決方案,希望這是有道理的,有人可以幫我一把。先謝謝你。這裏是我的代碼:

using System.Windows; 
using System.Xml.Linq; 
using System.Xml.XPath; 

namespace XMLwpfTest1 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     XDocument doc = new XDocument(
      new XElement("Content", 
      new XElement("Title", "DM Title")) 
     ); 

     string par = "par-"; 

     int counter = 1; 
     int counter2 = 1; 
     string stepNumber = ""; 



     public MainWindow() 
     { 
      InitializeComponent(); 
     } 

     private void create_Click(object sender, RoutedEventArgs e) 
     { 
      doc.Element("Content").Add(
       new XElement("proceduralStep", 
       new XAttribute("id", par + counter.ToString("D4")), 
       new XElement("para", 
      dataBox.Text))); 
      stepNumber = par + counter.ToString("D4"); 
      counter += 1; 
      dataBox.Clear(); 
      counter2 = 1; 
     } 


     private void createSubStep_Click(object sender, RoutedEventArgs e) 
     { 

      var addStep = doc.XPathSelectElement("Content/proceduralStep").Attribute(stepNumber); 


      addStep.add(
      new XElement("proceduralStep", 
      new XAttribute("id", stepNumber + "-" + counter2.ToString("D4")), 
      new XElement("para", 
     subDataBox.Text))); 
      counter2 += 1; 
      subDataBox.Clear(); 
     } 

     private void save_Click(object sender, RoutedEventArgs e) 
     { 
      doc.Save(fileName.Text + ".xml"); 
      savedLabel.Visibility = Visibility.Visible; 
     } 
    } 
} 
+0

這是一個完整的xml文件嗎?內容/程序步驟和內容來自哪裏?我沒有在xml中看到它們。 – Sybren

+0

我在問題中提出的XML只是我正在尋找的結構。這些不是實際的標籤名稱。我應該把我使用的實際標籤名稱。對困惑感到抱歉。 是將要創建的元素。 – swm462

回答

0

你的問題就來了,在這條線:

var addStep = doc.XPathSelectElement("Content/proceduralStep").Attribute(stepNumber); 

看起來你要選擇的<proceduralStep>id屬性具有價值stepNumber。要使用XPath做到這一點,你需要使用的語法[@attributeName='attributeValue'],即:

var addStep = doc.XPathSelectElement(string.Format("Content/proceduralStep[@id='{0}']", stepNumber)); 

需要注意的是,如果stepNumber是一個用戶輸入的字符串,你必須要小心,以防止XPath injection,例如通過按照說明here

或者,您也可以做到這一點使用通過附加Where表達:

var addStep = doc.XPathSelectElements("Content/proceduralStep") 
    .Where(e => (string)e.Attribute("id") == stepNumber) 
    .FirstOrDefault(); 

XPath注入不使用這種方法的問題。

(您當前正在打電話,XElement.Attribute(XName)的方法,返回屬性用指定的名稱,你不想要的。)

然後add()必須正確大寫爲XElement.Add()

addStep.Add(
new XElement("proceduralStep", 
new XAttribute("id", stepNumber + "-" + counter2.ToString("D4")), 
new XElement("para", subDataBox.Text))); 
+0

我有和以前一樣的錯誤信息,在'addStep.add'(' – swm462

+0

@ swm462 - 'XElement'上沒有方法'add()',它是['Add'](https:// msdn.microsoft.com/en-us/library/system.xml.linq.xelement.add(v=vs.110).aspx)。你在別處正確地做到了這一點。 – dbc

+0

你是對的!原諒我,我很安靜它不會完全符合我的需求,謝謝! – swm462

相關問題