如何重新序列化修改后的对象并插入回 XDocument 中?

发布于 2024-12-27 18:01:27 字数 2136 浏览 0 评论 0原文

我有一组在 XDocument 中表示的嵌套对象,如下所示:

<Record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="FooInfo">
  <Name>Red</Name>
  <Record xsi:type="BarInfo">
    <Name>Tomato</Name>
    <Record xsi:type="BazInfo">
      <Name>Juice</Name>
    </Record >
    <Record xsi:type="BazInfo">
      <Name>Sauce</Name>
    </Record >
  </Record >
</Record >

我查询 _doc.Descendents() 并反序列化为对象 BazInfo

 var info = doc.Descendants().Where(d => d.Attributes(xn).FirstOrDefault() != null)
                             .Where(d => d.Attribute(xn).Value == "BazInfo")
                             .Where(d => d.Element(name).Value == "Sauce");

 int count = info .Count();

 var e = info.First();
 BazInfo bi = (BazInfo)s.Deserialize(e.CreateReader());

BazInfo 类如下所示

class BazInfo : IInfo {  
    [XmlElement]
    string Name { get; set; }
    [XmlElement]
    string Manufacturer { get; set; }
}

BazInfo 最初是序列化的,未包含 Manufacturer 属性 - 即没有 元素 - 因为未分配任何值。这不是问题,只是当我想分配一个值时,我不能只在后代或元素集合中查找该元素。因此,我反序列化该对象并分配值:

bi.Manufacturer = "Acme"; 
// re-serialize element
s.Serialize(e.CreateWriter(), bi);

当我尝试重新序列化该对象时,我收到一个异常:

System.InvalidOperationException:生成错误 XML 文档。 --->系统.InvalidOperationException: 无法在使用以下命令创建的编写器上调用 WriteStartDocument ConformanceLevel.Fragment。

我希望能够将对象插入到它所属的位置:

  <Record xsi:type="BarInfo">
    <Name>Tomato</Name>
    <Record xsi:type="BazInfo">
      <Name>Juice</Name>
    </Record >
    <Record xsi:type="BazInfo">
      <Name>Sauce</Name>
      <Manufacturer>Acme</Manufacturer>
    </Record >
  </Record >

有什么想法吗?

我知道如果找不到,我可以添加一个元素“Manufacturer”,然后分配该值,但这似乎很容易被破坏。

I have a nested set of objects represented in an XDocument as such:

<Record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="FooInfo">
  <Name>Red</Name>
  <Record xsi:type="BarInfo">
    <Name>Tomato</Name>
    <Record xsi:type="BazInfo">
      <Name>Juice</Name>
    </Record >
    <Record xsi:type="BazInfo">
      <Name>Sauce</Name>
    </Record >
  </Record >
</Record >

I query the _doc.Descendents() and deserialize to the object BazInfo:

 var info = doc.Descendants().Where(d => d.Attributes(xn).FirstOrDefault() != null)
                             .Where(d => d.Attribute(xn).Value == "BazInfo")
                             .Where(d => d.Element(name).Value == "Sauce");

 int count = info .Count();

 var e = info.First();
 BazInfo bi = (BazInfo)s.Deserialize(e.CreateReader());

The BazInfo class looks like:

class BazInfo : IInfo {  
    [XmlElement]
    string Name { get; set; }
    [XmlElement]
    string Manufacturer { get; set; }
}

When BazInfo is initially serialized, the Manufacturer property was not included - i.e. no <Manufacturer /> element - because no value has been assigned. That isn't a problem except that when I want to assign a value, I can't just look up that element in the descendents or elements collections. So, I deserialize the object and assign the value:

bi.Manufacturer = "Acme"; 
// re-serialize element
s.Serialize(e.CreateWriter(), bi);

When I try to re-serialize the object, I receive an exeption:

System.InvalidOperationException: There was an error generating the
XML document. ---> System.InvalidOperationException:
WriteStartDocument cannot be called on writers created with
ConformanceLevel.Fragment.

I want to be able to insert the object back where it belongs:

  <Record xsi:type="BarInfo">
    <Name>Tomato</Name>
    <Record xsi:type="BazInfo">
      <Name>Juice</Name>
    </Record >
    <Record xsi:type="BazInfo">
      <Name>Sauce</Name>
      <Manufacturer>Acme</Manufacturer>
    </Record >
  </Record >

Any ideas?

I know I could add an element "Manufacturer" if not found and then assign the value, but that seems like it could get broken easily.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文