2012-05-18 48 views
0

所以我有以下代碼:的XElement/LinQ將代碼故障

public static void Replace(filepath) 
{ 
    try 
    { 
     XElement xD = XElement.Load(filePath); 
     foreach (XElement xE in xD.Elements()) 
     { 
      Console.WriteLine(xE.Attribute("attr").Value); 
      if (tuv.Attribute("attr") != null) 
      { 
       Console.WriteLine(xE.Attribute("attr").Value); 
       if (Regex.IsMatch(xE.Attribute("attr").EndsWith("AA")) 
       { 
        Console.WriteLine("match"); 
        tuv.Attribute("attr").Value.Replace("AA", ""); 
       } 
       Console.WriteLine(xE.Attribute("attr").Value); 
      } 
     } 
    } 
    catch (Exception e) 
    { 
     Console.WriteLine("Failure in Replace: {0}", e.ToString()); 
    } 
} 

而我得到的錯誤是:失敗在替換:System.NullReferenceException:對象不設置到對象的引用。 在Application.Program.Replace(字符串文件路徑)中:第21行(第一個Console.WriteLine)

這一計劃的目的是在滿足一定標準的XML文件進行編輯的任何屬性名...所以舉例來說,假設我們有:

<element attr="brAA"></element> 

這將被編輯成:據我所知

<element attr="br"></element> 

,我創建一個變量xE的代表元素的集合的內容xD.Elements()...我一直在打開我的頭現在一個小時!有沒有人有任何見解,爲什麼我會得到這個錯誤?

非常感謝!

這裏是XML

<body> 
    <par> 
    <prop type="Doc">1</prop> 
    <elem attr="aaaa"> 
     <child>REDACTED</child> 
    </elem> 
    <elem attr="aaAA"> 
     <child>REDACTED</child> 
    </elem> 
    <elem lang="abaa"> 
     <child>REDACTED</child> 
    </elem> 
    <elem attr="abAA"> 
     <child>REDACTED</child> 
    </elem> 
    <elem attr="acaa"> 
     <child>REDACTED</child> 
    </elem> 
    </par> 
</body> 
+1

我們可以看到XML嗎?我懷疑至少有一個正在迭代的元素缺少期望的屬性。 –

+0

這是一個1 gig文件...會有片段幫助嗎? – gfppaste

+0

'tuv'變量來自哪裏?複製並粘貼錯誤? –

回答

1

你通過中所有元素,並顯示「ATTR」屬性的值的片段。但是有些節點沒有「attr」屬性,因此是錯誤的。取下Console.WriteLineif塊的,它應該工作:

public static void Replace(filepath) 
{ 
    try 
    { 
     XElement xD = XElement.Load(filePath); 
     foreach (XElement xE in xD.Descendants()) 
     { 
      if (xE.Attribute("attr") != null) 
      { 
       Console.WriteLine(xE.Attribute("attr").Value); 
       if (Regex.IsMatch(xE.Attribute("attr").Value)) 
       { 
        Console.WriteLine("match"); 
        xE.Attribute("attr").Value.Replace("AA", ""); 
       } 
       Console.WriteLine(xE.Attribute("attr").Value); 
      } 
     } 
    } 
    catch (Exception e) 
    { 
     Console.WriteLine("Failure in Replace: {0}", e.ToString()); 
    } 
} 
+0

所以它不再拋出錯誤,但現在它不會移過第一個if語句>(如果xE.Attribute(「attr」)!= null):我在語句處設置了一個斷點,該程序停止...沒有寫入控制檯。 – gfppaste

+0

對,我錯過了那一個。 'xD.Elements()'只會返回樹的第一層。改用'xD.Descendants()'。我編輯了我的答案。 –

+1

@KooKiz - Replace語句(從原始代碼複製而來)實際上並不代替任何東西。 – Robaticus

1

你只需要對那些<elem>和你實際需要更換屬性值的元素工作。你的.Replace()沒有這樣做。

下面通過<elem>元素迭代的代碼,並且具有正確的更換:xE.Attribute("attr").Value = xE.Attribute("attr").Value.Replace("AA", "");

我也改變了你的.EndsWith擺脫正則表達式匹配。最後,有沒有錯誤處理在這裏爲缺少的屬性。你也應該檢查一下。

foreach (XElement xE in xD.Descendants("elem")) 
    { 
     //Console.WriteLine(xE.Attribute("attr").Value); 
     if (xE.Attribute("attr") != null) 
     { 
      Console.WriteLine(xE.Attribute("attr").Value); 
      if (xE.Attribute("attr").Value.EndsWith("AA")) 
      { 
       Console.WriteLine("match"); 
       xE.Attribute("attr").Value = xE.Attribute("attr").Value.Replace("AA", ""); 
      } 
      Console.WriteLine(xE.Attribute("attr").Value); 
     } 
    } 

* 編輯 - 你問我如何寫出來的文件。這將覆蓋它。

using(var sw = new StreamWriter(filepath, false)) 
    { 
     sw.Write(xD.ToString()); 
     sw.Close(); 
    } 
+0

嗯,現在它正在審閱文件,並使所需的內容發生變化,但是ti並沒有實際編輯文件本身。任何想法爲什麼會發生? – gfppaste

+0

它只在內存中。如果你想保存,你將不得不重新保存文件。 – Robaticus

+0

我從來沒有嘗試過...什麼是最好的方式來做到這一點? – gfppaste