这看起来很奇怪(除非我遗漏了一些明显的东西)。我必须存储这个字符串:
{TestField1:=TestAltField1}{TestField2:=TestAltField2}
在 XML 值字段中,结果如下:
<ResultFieldAlias>{TestField1:=TestAltField1}{TestField2:=TestAltField2}</ResultFieldAlias>
该值将存储在字符串、字符串字典中。但是,当我尝试在 foreach 循环和反射中将其从 XElement“XML”的 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 函数,它需要带有花括号和 := 的格式。当我调试它时,如果我查看循环中的 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 的 Last 元素创建一个全新的 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}"
是什么赋予了?谢谢。
更新:
为了回答 Jeffs 的评论并添加信息,将 XML 对象 (XElement) 传递给此函数。使用名为“ResultFieldAlias”的字符串字典的 Add 方法添加 TestField,然后使用此函数将其转换为字符串,然后再由 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}"