- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 XmlAttributeOverrides
来更改我的类序列化为 XML 的方式。我需要排除一些属性并按特定顺序包含其他属性。
我这里有这段代码:
// XML Attribute Overrrides
public static XmlAttributeOverrides GetXMLAttributeOverrides(Type theType, List<string> propertiesToInlcudeInOrder, List<string> allColumnNames)
{
try
{
if (propertiesToInlcudeInOrder != null)
{
XmlAttributeOverrides theXMLAttributeOverrides = new XmlAttributeOverrides();
if (propertiesToInlcudeInOrder.Count > 0)
{
XmlAttributes mainNewXMLAttributes = new XmlAttributes();
mainNewXMLAttributes.XmlIgnore = false;
XmlAttributes ignoreXMLAttributes = new XmlAttributes();
ignoreXMLAttributes.XmlIgnore = true;
List<string> propertiesToNotInclude = new List<string>();
foreach (string theColumnName in allColumnNames)
{
string thePropertyName = theColumnName;
bool addProperty = true;
foreach (string propertyToInclude in propertiesToInlcudeInOrder)
{
if (thePropertyName == propertyToInclude)
{
addProperty = false;
break;
}
}
if (addProperty)
{
propertiesToNotInclude.Add(thePropertyName);
}
}
// To Ignore
foreach (string propertyNameToNotInlcude in propertiesToNotInclude)
{
XmlElementAttribute theXMLElementAttributeToAdd = new XmlElementAttribute(propertyNameToNotInlcude);
theXMLElementAttributeToAdd.ElementName = propertyNameToNotInlcude;
ignoreXMLAttributes.XmlElements.Add(theXMLElementAttributeToAdd);
theXMLAttributeOverrides.Add(theType, propertyNameToNotInlcude, ignoreXMLAttributes);
}
// To Add In Order
int counter = 1;
foreach (string propertyNameToIncludeInOrder in propertiesToInlcudeInOrder)
{
XmlElementAttribute theXMLElementAttributeToAdd = new XmlElementAttribute(propertyNameToIncludeInOrder);
theXMLElementAttributeToAdd.ElementName = propertyNameToIncludeInOrder;
theXMLElementAttributeToAdd.Order = counter;
mainNewXMLAttributes.XmlElements.Add(theXMLElementAttributeToAdd);
theXMLAttributeOverrides.Add(theType, propertyNameToIncludeInOrder, mainNewXMLAttributes);
counter++;
}
}
return theXMLAttributeOverrides;
}
else
{
return null;
}
}
catch (Exception ex)
{
MessageBox.Show("Error at 'GetXMLAttributeOverrides'" + Environment.NewLine + Environment.NewLine + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return null;
}
}
在我的测试中,我有一个包含 13 个属性的类,我想按特定顺序包含 3 个并排除所有其他属性。
我已确保我的列表中没有任何重复项。我已经仔细检查过我在“忽略列表”和“包含列表”中没有相同的属性名称。我的这行代码出现异常:XmlSerializer(dataToSerialize.GetType(), allXMLAttribueOverrides);
allXMLAttribueOverrides
从我的方法 GetXMLAttributeOverrides
异常(exception)是:
There was an error reflecting type 'System.Collections.Generic.List`1[SystemName.UserControls.TestMain]'.
内部异常是:
There was an error reflecting property 'TextColumn'.
You need to add XmlChoiceIdentifierAttribute to the 'TextColumn' member.
'TextColumn' 是我测试类中的第一个属性
这是我的测试代码:
TestMain testItem = new TestMain(null, "TextColumnTEST", 5, Convert.ToDecimal(0.333), Convert.ToDecimal(0.777), DateTime.Now, "12:00:00", DateTime.Now, true, "Password", "#FFFFFF", null, null, null);
List<TestMain> dataToSerialize = new List<TestMain>();
dataToSerialize.Add(testItem);
List<string> propertiesToInlcudeInOrder = new List<string>();
propertiesToInlcudeInOrder.Add("CurrencyColumn");
propertiesToInlcudeInOrder.Add("NumberColumn");
propertiesToInlcudeInOrder.Add("TextColumn");
List<string> allColumnNames = new List<string>();
allColumnNames.Add("ID");
allColumnNames.Add("Select");
allColumnNames.Add("TextColumn");
allColumnNames.Add("NumberColumn");
allColumnNames.Add("CurrencyColumn");
allColumnNames.Add("DecimalColumn");
allColumnNames.Add("DateColumn");
allColumnNames.Add("TimeColumn");
allColumnNames.Add("DateAndTimeColumn");
allColumnNames.Add("YesNoColumn");
allColumnNames.Add("PasswordColumn");
allColumnNames.Add("ColorColumn");
allColumnNames.Add("ImageColumn");
allColumnNames.Add("DocumentColumn");
allColumnNames.Add("OtherColumn");
XmlAttributeOverrides allXMLAttribueOverrides = ReportingManipulation.GetXMLAttributeOverrides(dataToSerialize[0].GetType(), propertiesToInlcudeInOrder, allColumnNames);
using (StringWriter mainStringWriter = new StringWriter())
{
XmlSerializer mainXMLSerializer = new XmlSerializer(dataToSerialize.GetType(), allXMLAttribueOverrides);
mainXMLSerializer.Serialize(mainStringWriter, dataToSerialize);
return mainStringWriter.ToString();
}
这是我的测试类:
public class TestMain
{
#region Properties
// Properties
[XmlIgnore]
public int? ID { get; set; }
[XmlIgnore]
public bool Select { get; set; }
public string TextColumn { get; set; }
public int NumberColumn { get; set; }
public decimal CurrencyColumn { get; set; }
public decimal DecimalColumn { get; set; }
public DateTime DateColumn { get; set; }
public string TimeColumn { get; set; }
public DateTime DateAndTimeColumn { get; set; }
public bool YesNoColumn { get; set; }
public string PasswordColumn { get; set; }
public string ColorColumn { get; set; }
public byte[] ImageColumn { get; set; }
public byte[] DocumentColumn { get; set; }
public byte[] OtherColumn { get; set; }
#endregion
#region Constructors
// Constructors
public TestMain()
{
try
{
}
catch (Exception ex)
{
MessageBox.Show("Error at Constructor: 'TestMain'" + Environment.NewLine + Environment.NewLine + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
public TestMain(int? theID, string theTextColumn, int theNumberColumn, decimal theCurrencyColumn, decimal theDecimalColumn, DateTime theDateColumn, string theTimeColumn, DateTime theDateAndTimeColumn, bool theYesNoColumn, string thePasswordColumn, string theColorColumn, byte[] theImageColumn, byte[] theDocumentColumn, byte[] theOtherColumn)
{
try
{
this.ID = theID;
this.TextColumn = theTextColumn;
this.NumberColumn = theNumberColumn;
this.CurrencyColumn = theCurrencyColumn;
this.DecimalColumn = theDecimalColumn;
this.DateColumn = theDateColumn;
this.TimeColumn = theTimeColumn;
this.DateAndTimeColumn = theDateAndTimeColumn;
this.YesNoColumn = theYesNoColumn;
this.PasswordColumn = thePasswordColumn;
this.ColorColumn = theColorColumn;
this.ImageColumn = theImageColumn;
this.DocumentColumn = theDocumentColumn;
this.OtherColumn = theOtherColumn;
}
catch (Exception ex)
{
MessageBox.Show("Error at Constructor: 'TestMain'" + Environment.NewLine + Environment.NewLine + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
#endregion
}
我哪里错了?
如有任何帮助/建议,我们将不胜感激。
最佳答案
您的基本问题 是您要添加多个覆盖 [XmlElement]
每个属性的属性,因为您对所有属性使用单个实例 mainNewXMLAttributes
,它累积为所有属性定义的 XmlElementAttribute
对象。
要解决此问题,您需要为 foreach(var propertyNameToIncludeInOrder in propertiesToInlcudeInOrder)
循环中的每个属性分配一个新的 mainNewXMLAttributes
,如显示在以下 GetXMLAttributeOverrides()
的更正和简化版本中:
public static partial class ReportingManipulation
{
public static XmlAttributeOverrides GetXMLAttributeOverrides(Type theType, IList<string> propertiesToInlcudeInOrder)
{
var allProperties = theType.GetProperties(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance).Select(p => p.Name);
return GetXMLAttributeOverrides(theType, propertiesToInlcudeInOrder, allProperties);
}
// XML Attribute Overrrides
public static XmlAttributeOverrides GetXMLAttributeOverrides(Type theType, IList<string> propertiesToInlcudeInOrder, IEnumerable<string> allProperties)
{
if (propertiesToInlcudeInOrder == null || propertiesToInlcudeInOrder.Count == 0)
return null;
var theXMLAttributeOverrides = new XmlAttributeOverrides();
// To Add In Order
int counter = 1;
foreach (var propertyNameToIncludeInOrder in propertiesToInlcudeInOrder)
{
// Allocate a fresh instance of XmlAttributes for each property, because we are defining a different
// XmlElementAttribute for each
var mainNewXMLAttributes = new XmlAttributes { XmlIgnore = false };
// Specify the element order XmlElementAttribute and attach to the XmlAttributes
var theXMLElementAttributeToAdd = new XmlElementAttribute { Order = counter };
mainNewXMLAttributes.XmlElements.Add(theXMLElementAttributeToAdd);
// Attach the override XmlElementAttribute to the property propertyNameToIncludeInOrder
theXMLAttributeOverrides.Add(theType, propertyNameToIncludeInOrder, mainNewXMLAttributes);
counter++;
}
// To Ignore
// Using System.Linq.Enumerable.Except()
var propertiesToNotInclude = allProperties.Except(propertiesToInlcudeInOrder);
var ignoreXMLAttributes = new XmlAttributes { XmlIgnore = true };
foreach (var propertyNameToNotInlcude in propertiesToNotInclude)
{
// Attach the override XmlElementAttribute to the property propertyNameToIncludeInOrder
// No need to allocate a fresh instance of ignoreXMLAttributes for each, because the instances would all be identical
theXMLAttributeOverrides.Add(theType, propertyNameToNotInlcude, ignoreXMLAttributes);
}
return theXMLAttributeOverrides;
}
}
为什么您的代码不起作用?在您的初始代码中,您:
XmlAttributes mainNewXMLAttributes = new XmlAttributes();
mainNewXMLAttributes.XmlIgnore = false;
int counter = 1;
foreach (string propertyNameToIncludeInOrder in propertiesToInlcudeInOrder)
{
XmlElementAttribute theXMLElementAttributeToAdd = new XmlElementAttribute(propertyNameToIncludeInOrder);
theXMLElementAttributeToAdd.ElementName = propertyNameToIncludeInOrder;
theXMLElementAttributeToAdd.Order = counter;
mainNewXMLAttributes.XmlElements.Add(theXMLElementAttributeToAdd);
theXMLAttributeOverrides.Add(theType, propertyNameToIncludeInOrder, mainNewXMLAttributes);
counter++;
}
现在,方法XmlAttributeOverrides.Add(Type, String, XmlAttributes)
被记录为按如下方式工作:
Adds an
XmlAttributes
object to the collection ofXmlAttributes
objects. Thetype
parameter specifies an object to be overridden. Themember
parameter specifies the name of a member that is overridden.
因此,当最终构造 XmlSerializer
时,mainNewXMLAttributes
的内容将应用于命名参数。当您只为所有参数构造 mainNewXMLAttributes
实例时,它的 XmlElements
数组将包含与 所有 参数相对应的元素名称! IE。您的代码尝试将多个 [XmlElement]
属性应用于每个命名参数,仅在重写名称和顺序上有所不同。这说明了您需要将 XmlChoiceIdentifierAttribute 添加到 'TextColumn' 成员。 异常 - 如果属性值是多态的并且您想要分配不同的元素名称,则只能将多个元素名称附加到属性到不同的值类型。
注意事项
生成带有覆盖的 XmlSerializer
时,您必须静态缓存它并在以后重用它以避免严重的内存泄漏,如 Memory Leak using StreamReader and XmlSerializer 中所述。 。
我不建议在低级实用方法或对象构造函数中无条件地吞下异常并将它们作为错误消息呈现给用户。
演示工作 fiddle here .
关于c# - : "You need to add XmlChoiceIdentifierAttribute to the member." when using XmlAttributeOverrides?如何解决,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54476647/
I have a question about adding files in git. I have found multiple stackoverflow questions about
我是 visual studio 的新手,来自 Delphi。 我有一个充满 .cs 文件的目录树(根是\Common)。 我还有一个充满应用程序的目录树(根目录是\Applications) 最后,
这个问题在这里已经有了答案: Array's lookup time complexity vs. how it is stored (4 个答案) Time complexity for java
谁能告诉我这两者有什么区别: ALTER TABLE x1 ADD INDEX(a); ALTER TABLE x1 ADD INDEX(b); 和 ALTER TABLE x1 ADD INDEX(
为什么有时我们使用 getChildren() add() 而其他时候我们直接使用 add() es: https://docs.oracle.com/javafx/2/get_started/for
如何使用 bootstrap css 在输入下方添加跨度?我需要做这样的事情: 最佳答案 是这样的吗? http://jsfiddle.net/swm53ran/205/ 您可以使用纯 CSS 来实现
问题 np.add(X, 2*Y, out=X) 比 np.add(X, Y, out=X); np.add(X, Y, out=X).使用 np.add(X, Y, out=X); 是一种实际做法吗
当我跑 git add --intent-to-add .所有未跟踪的文件将其状态从“未跟踪的文件”( git status -s 显示 ?? )更改为“未暂存以进行提交的更改”( git statu
我不知道 .add 之间有什么区别和 .sink.add ? 例子: StreamController myStreamController = StreamController(); stream
getContentPane().add() 和 add() 的意思一样吗? public class TestFrame extends JFrame{ public TestFrame()
git add . 和 git add * 会完成完全相同的事情吗? 最佳答案 不,不会。 * 是一个 glob 模式,不会匹配以 开头的文件。 例如,假设这是当前目录,我有 2 个新文件要添加 fo
git的分支与合并的两种方法 git add -A和 git add . git add -u在功能上看似很相近,但还是存在一点差别 git add . :他会
git add [--all | -A] 之间有什么区别?和 git add . ? 最佳答案 此答案仅适用于 Git 版本 1.x。对于 Git 版本 2.x,请参阅其他答案。 总结: git ad
我刚刚安装了最新的 Wix v3.7。我创建了一个 VS 2010“Excel 2010 加载项”项目,并在同一个解决方案中创建了一个 Wix“安装项目”。 问题是,当我尝试从 Wix 项目中引用 A
YUI.add 和 YUI().add 有什么区别? 最佳答案 在第一种情况下,您要注册一个模块可以加载到 YUI 沙箱中,在第二种情况下,您要构建一个沙箱,然后进行注册(这是一种非常不典型的用法)。
测试代码时,任何输入到列表中的值在按下“enter”后都会消失。 我对编程和网络开发非常陌生。请具体一点,以便我理解。 function addItem(){ var item = documen
我正在浏览 python 的 dis 包。我尝试了代码以查看它是如何工作的 >>> def get(): ... x=4 ... y=x+3 ............ this lin
我已经对我的文件夹进行了版本控制 git init git add . git commit -m 'Initial commit' 我应该怎么做 git add 对于我在 .? 中创建的每个新文件
当我执行 $ git add * 时,有时我意识到 git 不会将已删除的文件添加到舞台上,如果删除或添加它,我需要手动指示,但我想不通找出 $ git add --all 有什么区别。因此,如果星号
这个问题在这里已经有了答案: Difference between "git add -A" and "git add ." (12 个答案) 关闭 6 年前。 目前,当我想提交并将内容推送到远程
我是一名优秀的程序员,十分优秀!