Linq To Xml 保存嵌套对象列表

发布于 2024-10-30 22:46:08 字数 1294 浏览 1 评论 0原文

我目前使用如下代码将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪之魂 2024-11-06 22:46:08

一方面,您只向我们展示了从 XElement 解析 的运算符...但即便如此,您显然还是在 LINQ 表达式中显式调用该运算符。如果您在构建 XML 时想要等效的内容,那么您也需要明确说明:

XElement xml = new XElement("Root",
                            from p in ObjectList
                            select new XElement("File", (XElement) p));

就我个人而言,我会使用方法而不是运算符 - 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:

XElement xml = new XElement("Root",
                            from p in ObjectList
                            select new XElement("File", (XElement) p));

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文