2013-05-07 40 views
0

這看起來很奇怪(除非我缺少明顯的東西)。我有這個字符串存儲:Linq XElement更改foreach中的值格式

{TestField1:=TestAltField1}{TestField2:=TestAltField2} 

,導致像這樣的XML值字段:

<ResultFieldAlias>{TestField1:=TestAltField1}{TestField2:=TestAltField2}</ResultFieldAlias> 

值是存儲在字符串,字符串字典。但是,當我試圖把它拉出來的XElement「XML」的foreach循環和反射的XElements的,它改變了支架和下降造成這種冒號:

[TestField1, TestAltField1][TestField2, TestAltField2] 

這裏是我使用的代碼通過反射獲取屬性:

PropertyInfo[] properties = GetPublicProperties(); 

//loop through each entry in the dictionary and try to find its matching property 
foreach (XE xe in XML.Elements()) 
{ 
    //Try to find a matching prop to this key 
    PropertyInfo prop = properties.FirstOrDefault(p => p.Name.ToLower() == xe.Name.ToString().ToLower()); 

    if (prop != null) 
     if (prop.PropertyType == typeof(bool)) 
      prop.SetValue(this, bool.Parse(xe.Value), null); 
     else if (prop.PropertyType == typeof(FontSettings)) 
      prop.SetValue(this, new FontSettings(xe), null); 
     else if (prop.PropertyType == typeof(Dictionary<string, string>)) 
      prop.SetValue(this, MyExtension.Deserialize(xe.Value), null); 
     else 
      prop.SetValue(this, xe.Value, null); 
} 

請注意Deserialize函數期望帶有curlies和:=的格式。當我調試它,我從即時窗口得到這個,如果我看在循環中的XE對象:

? xe 
<ResultFieldAlias>[TestField1, TestAltField1][TestField2, TestAltField2]</ResultFieldAlias> 
    base {System.Xml.Linq.XContainer}: <ResultFieldAlias>[TestField1, TestAltField1][TestField2, TestAltField2]</ResultFieldAlias> 
    FirstAttribute: null 
    HasAttributes: false 
    HasElements: false 
    IsEmpty: false 
    lastAttr: null 
    LastAttribute: null 
    Name: {ResultFieldAlias} 
    name: {ResultFieldAlias} 
    NodeType: Element 
    Value: "[TestField1, TestAltField1][TestField2, TestAltField2]" 

,但如果我創建基於關閉XML的最後一個元素一個全新的XElement(它發生在地面上,之前有一堆),我得到正確的格式:

? XElement xe1 = XML.Elements().Last(); 
<ResultFieldAlias>{TestField1:=TestAltField1}{TestField2:=TestAltField2}</ResultFieldAlias> 
    base {System.Xml.Linq.XContainer}: <ResultFieldAlias>{TestField1:=TestAltField1}{TestField2:=TestAltField2}</ResultFieldAlias> 
    FirstAttribute: null 
    HasAttributes: false 
    HasElements: false 
    IsEmpty: false 
    lastAttr: null 
    LastAttribute: null 
    Name: {ResultFieldAlias} 
    name: {ResultFieldAlias} 
    NodeType: Element 
    Value: "{TestField1:=TestAltField1}{TestField2:=TestAltField2}" 

什麼給?謝謝。

UPDATE:

要回答傑夫斯評論和補充信息,XML對象(的XElement)被傳遞給這個函數。的TestFields使用一個字符串的添加方法添加,串字典稱爲「ResultFieldAlias」,然後將其由XML標籤「ResultFieldAlias」被包裹之前被轉換爲使用該功能的字符串:

public static string Serialize(Dictionary<string, string> data) 
{ 
    if (data == null) 
     return null; 

    return "{" + String.Join("}{", data.Select(c => c.Key + ":=" + c.Value).ToArray()) + "}"; 
} 

結果是如如下(只是它的尾部 - 這將是巨大的張貼):

<Callouts> 
    ..... 
    ..... 
    <ResultExceedFont> 
    <FontSettings> 
     <FontName>Curlz MT</FontName> 
     <FontColor>#FF6B8E23</FontColor> 
     <FontSize>15</FontSize> 
     <FontUnderline>false</FontUnderline> 
     <FontBold>false</FontBold> 
     <FontItalic>true</FontItalic> 
    </FontSettings> 
    </ResultExceedFont> 
    <ResultFieldAlias>{TestField1:=TestAltField1}{TestField2:=TestAltField2}</ResultFieldAlias> 
</Callouts> 

兩個前面的代碼粘貼從我在VS立即窗口有(與在開始的開始?)採取在行中斷「prop.SetValue(this,MyExtension.Deserialize(xe.Value),null);」所以他們在同一時間點擊相同的XML對象。這裏是準確的剪切和粘貼(再次縮短XML轉儲):

? XML 
.... 
    <ResultExceedFont> 
    <FontSettings> 
     <FontName>Curlz MT</FontName> 
     <FontColor>#FF6B8E23</FontColor> 
     <FontSize>15</FontSize> 
     <FontUnderline>false</FontUnderline> 
     <FontBold>false</FontBold> 
     <FontItalic>true</FontItalic> 
    </FontSettings> 
    </ResultExceedFont> 
    <ResultFieldAlias>{TestField1:=TestAltField1}{TestField2:=TestAltField2}</ResultFieldAlias> 
</Callouts> 
    FirstAttribute: null 
    HasAttributes: false 
    HasElements: true 
    IsEmpty: false 
    lastAttr: null 
    LastAttribute: null 
    Name: {Callouts} 
    name: {Callouts} 
    NodeType: Element 
    Value: "falsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalse[TestField1, TestAltField1][TestField2, TestAltField2]falsefalseBalloonfalse#FFFFFF00Adobe Song Std#FF228B2210truetruetrueAdobe Ming Std#FFF0808010truetruetrueAdobe Kaiti Std#FFDC143C20truefalsetrueCurlz MT#FF6B8E2315falsefalsetrue{TestField1:=TestAltField1}{TestField2:=TestAltField2}" 
?xe 
<ResultFieldAlias>[TestField1, TestAltField1][TestField2, TestAltField2]</ResultFieldAlias> 
    base {System.Xml.Linq.XContainer}: <ResultFieldAlias>[TestField1, TestAltField1][TestField2, TestAltField2]</ResultFieldAlias> 
    FirstAttribute: null 
    HasAttributes: false 
    HasElements: false 
    IsEmpty: false 
    lastAttr: null 
    LastAttribute: null 
    Name: {ResultFieldAlias} 
    name: {ResultFieldAlias} 
    NodeType: Element 
    Value: "[TestField1, TestAltField1][TestField2, TestAltField2]" 
?XML.Elements().Last() 
<ResultFieldAlias>{TestField1:=TestAltField1}{TestField2:=TestAltField2}</ResultFieldAlias> 
    base {System.Xml.Linq.XContainer}: <ResultFieldAlias>{TestField1:=TestAltField1}{TestField2:=TestAltField2}</ResultFieldAlias> 
    FirstAttribute: null 
    HasAttributes: false 
    HasElements: false 
    IsEmpty: false 
    lastAttr: null 
    LastAttribute: null 
    Name: {ResultFieldAlias} 
    name: {ResultFieldAlias} 
    NodeType: Element 
    Value: "{TestField1:=TestAltField1}{TestField2:=TestAltField2}" 
+1

的LINQ to XML不會做出這樣的更改數據。問題在於你如何解析它。 XML是如何初始化的? – 2013-05-07 22:14:24

回答

0

NEVERMIND! Ahhh..stupid ....它發現我在構建XML時添加了ResultFieldAlias字典TWICE時沒有正確過濾。第一個實例是由字典的默認ToString()方法創建的 - 因此是格式化。所以xe var抓住了那個,因爲它是第一個,但是我的.Last()測試抓住了第二個正確的版本。

對不起,沒有用的帖子。謝謝Jeff讓我仔細觀察。

厄尼