Linq To Xml 保存嵌套对象列表
我目前使用如下代码将 XML 文件加载到列表对象中。
XDocument xmlDoc = XDocument.Load(path);
List<ImportDefinition> importDefinitions = xmlDoc.Descendants("Root").Select(xElem => (ImportDefinition)xElem).ToList();
return importDefinitions;
此对象列表包含嵌套对象,每个对象都有一个运算符,用于将 XML 解析为正确的形式,如下所示。
public static explicit operator Rules(XElement xElem)
{
try
{
return new Rules()
{
FileNameRegEx = (string)xElem.Element("FileNameRegEx"),
FileExtension = (string)xElem.Element("FileExtension")
};
}
catch (Exception ex)
{
return null;
}
这对于加载 XML 效果很好。现在,我想在进行一些编辑后将此对象列表保存回 XML。
我希望这样的东西能够工作
XElement xml = new XElement("Root",
from p in ObjectList
select new XElement("File",RootObject
));
}
xml.Save("C:\\temp\\newimport.xml");
但是这似乎只是输出这个
<?xml version="1.0" encoding="utf-8"?>
<Root>
<File>MyNamespace.RootObject</File>
<File>MyNamespace.RootObject</File>
</Root>
看起来它没有使用加载文件时使用的自定义运算符来计算出保存的格式。将此数据保存回 XML 的最佳方法是什么与我阅读时的格式相同吗?
I currently load an XML file into a list objects using code like this
XDocument xmlDoc = XDocument.Load(path);
List<ImportDefinition> importDefinitions = xmlDoc.Descendants("Root").Select(xElem => (ImportDefinition)xElem).ToList();
return importDefinitions;
This list of objects contains nested objects and each one has an operator for parsing the XML into the correct form like this
public static explicit operator Rules(XElement xElem)
{
try
{
return new Rules()
{
FileNameRegEx = (string)xElem.Element("FileNameRegEx"),
FileExtension = (string)xElem.Element("FileExtension")
};
}
catch (Exception ex)
{
return null;
}
This works fine for loading the XML. I now want to save this list of objects back to XML after some edits have been made.
I was hoping something like this would work
XElement xml = new XElement("Root",
from p in ObjectList
select new XElement("File",RootObject
));
}
xml.Save("C:\\temp\\newimport.xml");
However this just seems to output this
<?xml version="1.0" encoding="utf-8"?>
<Root>
<File>MyNamespace.RootObject</File>
<File>MyNamespace.RootObject</File>
</Root>
It looks like its not using the custom operators it uses when loading the files to work out the format to save in. Whats the best way to save this data back to XML to the same format it was in when I read it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一方面,您只向我们展示了从 XElement 解析 的运算符...但即便如此,您显然还是在 LINQ 表达式中显式调用该运算符。如果您在构建 XML 时想要等效的内容,那么您也需要明确说明:
就我个人而言,我会使用方法而不是运算符 - ToXElement() 和 FromXElement() - 我认为这样更清晰。
ToXElement
将是一个实例方法;FromXElement
将是一个静态方法。这是我多次使用过的模式,而且总是效果很好。Well for one thing you've only shown us the operator for parsing from an XElement... but even so, you're obviously explicitly calling that in your LINQ expression. If you want the equivalent when building XML, you'll need to be explicit there too:
Personally I'd use methods instead of operators - ToXElement() and FromXElement() - I think it's clearer that way.
ToXElement
would be an instance method;FromXElement
would be a static method. This is a pattern I've used many times, and it's always worked fine.