DSL 将根元素添加到序列化中

发布于 2024-08-01 23:17:07 字数 533 浏览 6 评论 0原文

我正在寻求帮助来实现以下目标

该图代表一辆汽车,

当我查看 XML 时,用户可以添加引擎和颜色,它看起来像这样:

<Car>
  <Engine>BigEngine</Engine>
  <Colour>Pink</Colour>
</Car>

我想做的是将汽车包装在“车辆”内,即

<Vehicle>
  <Car>
    <Engine>BigEngine</Engine>
    <Colour>Pink</Colour>
  </Car>
</Vehicle>

我是不确定实现这一目标的最佳方法。 我希望将模型资源管理器和生成的 XML 包装在“车辆”中,但出于所有其他意图和目的,用户仅使用汽车

信息:Visual Studio 2010、C# 和 DSL SDK for 2010

I am looking for help to achieve the following

The Diagram represents a car, users can add engine and colour

when I view the XML it looks like this:

<Car>
  <Engine>BigEngine</Engine>
  <Colour>Pink</Colour>
</Car>

What I would like to do is to wrap the car inside 'vehicle', i.e

<Vehicle>
  <Car>
    <Engine>BigEngine</Engine>
    <Colour>Pink</Colour>
  </Car>
</Vehicle>

I am not sure of the best way to achieve this. I want the model explorer and the generated XML to be wrapped in 'vehicle' but for all other intents and purposes the user is working with a car only

Info: Visual Studio 2010, C# and DSL SDK for 2010

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

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

发布评论

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

评论(2

愁杀 2024-08-08 23:17:07

我已经通过以下方法解决了这个问题。 我双重派生 Car 类,在 Car 序列化器中我正在执行以下操作:

编写额外的元素:

  public partial class CarSerializer : CarSerializerBase
{
    public override void Write(SerializationContext serializationContext, ModelElement element, XmlWriter writer, RootElementSettings rootElementSettings)
    {
        // Adds the Model and LobSystem root elements to match that required by the SharePoint BCS
        writer.WriteStartElement("Garage");
        writer.WriteStartElement("Cars");
        base.Write(serializationContext, element, writer, rootElementSettings);
        writer.WriteEndElement();
        writer.WriteEndElement();
    }
}

为了能够读回此内容,我重写了 SerializationHelper 中的 Car LoadModel 方法以及它在哪里获取我正在阅读的读者直到我到达汽车。

......

XmlReader reader = XmlReader.Create(fileStream, settings);
reader.MoveToContent();
while (!reader.EOF && !reader.Name.Equals("Car"))
{
   reader.Read();
}

reader = reader.ReadSubtree();

//    using (global::System.Xml.XmlReader reader = global::System.Xml.XmlReader.Create(fileStream, settings))
using (reader)
{

I have fixed this by the following. I am double deriving the Car class and in the Car serializer I am doing this:

Writing the extra elements:

  public partial class CarSerializer : CarSerializerBase
{
    public override void Write(SerializationContext serializationContext, ModelElement element, XmlWriter writer, RootElementSettings rootElementSettings)
    {
        // Adds the Model and LobSystem root elements to match that required by the SharePoint BCS
        writer.WriteStartElement("Garage");
        writer.WriteStartElement("Cars");
        base.Write(serializationContext, element, writer, rootElementSettings);
        writer.WriteEndElement();
        writer.WriteEndElement();
    }
}

To be able to read this back in I am overriding the Car LoadModel method in the SerializationHelper and where it is getting the reader I am reading the elements until I get to Car.

....

XmlReader reader = XmlReader.Create(fileStream, settings);
reader.MoveToContent();
while (!reader.EOF && !reader.Name.Equals("Car"))
{
   reader.Read();
}

reader = reader.ReadSubtree();

//    using (global::System.Xml.XmlReader reader = global::System.Xml.XmlReader.Create(fileStream, settings))
using (reader)
{

....

温柔少女心 2024-08-08 23:17:07

我会尝试两种不同的方法:


第一种:覆盖 DSL 包类 DocData
在 DocData.cs 文件和重写方法中

protected override void OnDocumentSaved(System.EventArgs e)

,然后我将创建包装器,

然后在 DocData.cs 中重写

protected override void OnDocumentLoading(System.EventArgs e)

,并在调用基本方法 base.OnDocumentLoading(e); 之前,我将从文件中删除。


第二:在 DSL Explorer 下,转到 XML Serialization Behaviour 并设置 Car Domain Class“Is Custom = true”。

这个解决方案并不简单,但也不像乍看起来那么复杂。 您必须定义每个方法,但对于每个自定义方法,您可以调用名为“DefaulMethod”的 DSL 生成方法,该方法具有默认的 DSL 序列化器行为。


我目前使用的是 VS 2005,所以有些事情可能已经改变了......

I would try two different approaches:


1st: override DSL Package class DocData
In DocData.cs file and override method

protected override void OnDocumentSaved(System.EventArgs e)

and then I would create the wrapper

afterwards I'd override in DocData.cs

protected override void OnDocumentLoading(System.EventArgs e)

and before calling the base method base.OnDocumentLoading(e); i would delete from the file.


2nd: Under DSL Explorer go to XML Serialization Behaviour and set Car Domain Class "Is Custom = true".

This solution is not straightforward but it's not as complicated as it seems at the first place. You'll must define every single method but for each custom method you can call a DSL generated method called "DefaulMethod" which has the default DSL serializer behaviour.


I am currently using VS 2005, so some things might have changed...

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