XML 反序列化为同一个对象不同的元素名称
XML deserialize to same object different element names
我正在尝试将 xml 反序列化为要使用的对象。我们已经创建了模板,并希望尽可能保持 xml 相同的标准。我想弄清楚的问题是如何在 xml 中的标准节点内查找,所有子节点都是相同的对象类型,只是节点名称不同。
例如:
<Account>
<AccountNumber>12345</AccountNumber>
<Balance>12.52</Balance>
<LateFee>0</LateFee>
</Account>
帐户级别始终在模板中,但低于该级别的所有内容都是可变的。有没有办法将帐户级别内的所有节点反序列化为同一对象?
Public Class AccountNode
{
Public String Name { get; set; }
Public String Value { get; set; }
}
根据我的研究,它们似乎必须有一个标准的命名模式,然后您可以有一个属性分配给 Name 值。我只是无法证实这一点。如果有人link我没能找到,或者有知识可以确认这是否可能,我想知道。
编辑:
我的 xml 比上面列出的要大得多,所以我想看看如何反序列化它。
<AccountNumber>
<KeyWord Name="Customer Account" isRegex="False" errorAllowance="10" LookFor="Customer Account">
<Rectangle>
<Left>200</Left>
<Bottom>350</Bottom>
<Right>600</Right>
<Top>690</Top>
</Rectangle>
<Relations KwName="Charges">
<Relation>above.0</Relation>
</Relations>
</KeyWord>
<Capture DataType="String" FindIfKeywordMissing="false">
<Rectangle>
<Left>200</Left>
<Bottom>350</Bottom>
<Right>600</Right>
<Top>690</Top>
</Rectangle>
<Relations anchorName="ChargeSection">
<Relation>rightOf.0</Relation>
<Relation>below.-20</Relation>
<Relation>above.20</Relation>
<Relation>width.150</Relation>
</Relations>
<Regex>Customer account\s+(\S+)</Regex>
</Capture>
</AccountNumber>
所以对于这个,我假设它是相似的,但基本上帐号节点是变量一,它上面和下面的所有内容都是标准的。
您可以在 Account
class 中使用代理项 [XmlAnyElement] public XElement[] AccountNodesXml
属性 手动将 AccountNode
对象从 XML 转换为 XML节点。用 XmlAnyElement
标记 属性 确保元素将从 XML:
逐字获取
public class Account
{
public Account() { this.AccountNodes = new List<AccountNode>(); }
[XmlIgnore]
public List<AccountNode> AccountNodes { get; set; }
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
[XmlAnyElement]
public XElement[] AccountNodesXml
{
get
{
if (AccountNodes == null)
return null;
return AccountNodes.Select(a => new XElement((XName)a.Name, a.Value)).ToArray();
}
set
{
if (value != null)
AccountNodes = value.Select(e => new AccountNode { Name = e.Name.LocalName, Value = (string)e }).ToList();
}
}
}
示例 fiddle 成功反序列化和重新序列化以下内容 XML:
<Account>
<AccountNumber>12345</AccountNumber>
<Balance>12.52</Balance>
<LateFee>0</LateFee>
</Account>
我正在尝试将 xml 反序列化为要使用的对象。我们已经创建了模板,并希望尽可能保持 xml 相同的标准。我想弄清楚的问题是如何在 xml 中的标准节点内查找,所有子节点都是相同的对象类型,只是节点名称不同。
例如:
<Account>
<AccountNumber>12345</AccountNumber>
<Balance>12.52</Balance>
<LateFee>0</LateFee>
</Account>
帐户级别始终在模板中,但低于该级别的所有内容都是可变的。有没有办法将帐户级别内的所有节点反序列化为同一对象?
Public Class AccountNode
{
Public String Name { get; set; }
Public String Value { get; set; }
}
根据我的研究,它们似乎必须有一个标准的命名模式,然后您可以有一个属性分配给 Name 值。我只是无法证实这一点。如果有人link我没能找到,或者有知识可以确认这是否可能,我想知道。
编辑:
我的 xml 比上面列出的要大得多,所以我想看看如何反序列化它。
<AccountNumber>
<KeyWord Name="Customer Account" isRegex="False" errorAllowance="10" LookFor="Customer Account">
<Rectangle>
<Left>200</Left>
<Bottom>350</Bottom>
<Right>600</Right>
<Top>690</Top>
</Rectangle>
<Relations KwName="Charges">
<Relation>above.0</Relation>
</Relations>
</KeyWord>
<Capture DataType="String" FindIfKeywordMissing="false">
<Rectangle>
<Left>200</Left>
<Bottom>350</Bottom>
<Right>600</Right>
<Top>690</Top>
</Rectangle>
<Relations anchorName="ChargeSection">
<Relation>rightOf.0</Relation>
<Relation>below.-20</Relation>
<Relation>above.20</Relation>
<Relation>width.150</Relation>
</Relations>
<Regex>Customer account\s+(\S+)</Regex>
</Capture>
</AccountNumber>
所以对于这个,我假设它是相似的,但基本上帐号节点是变量一,它上面和下面的所有内容都是标准的。
您可以在 Account
class 中使用代理项 [XmlAnyElement] public XElement[] AccountNodesXml
属性 手动将 AccountNode
对象从 XML 转换为 XML节点。用 XmlAnyElement
标记 属性 确保元素将从 XML:
public class Account
{
public Account() { this.AccountNodes = new List<AccountNode>(); }
[XmlIgnore]
public List<AccountNode> AccountNodes { get; set; }
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
[XmlAnyElement]
public XElement[] AccountNodesXml
{
get
{
if (AccountNodes == null)
return null;
return AccountNodes.Select(a => new XElement((XName)a.Name, a.Value)).ToArray();
}
set
{
if (value != null)
AccountNodes = value.Select(e => new AccountNode { Name = e.Name.LocalName, Value = (string)e }).ToList();
}
}
}
示例 fiddle 成功反序列化和重新序列化以下内容 XML:
<Account>
<AccountNumber>12345</AccountNumber>
<Balance>12.52</Balance>
<LateFee>0</LateFee>
</Account>