Question

How do you set a value of an xml property?

This is what I've tried with no succes:

<property name="Resources" value="&#60;resources/&#62;"/>

Resources is an XmlDocument field.

Was it helpful?

Solution

Just to clarify: This property is on an object that has a field that is an XmlDocument, and you want it to be initialized to an empty XmlDocument with the root element "resources".

XmlDocuments aren't always the easiest objects to work with, especially when it comes to construction.

Spring for sure won't know how to turn a string into an XmlDocument.

You might find it easier to use code to generate the XmlDocument you want. For example, create a static helper method that generates the XmlDocument, and set the value of the property by invoking that method.

Another (kludge-y) option is to have a "helper" property that deals with xml as string. For example, a property called "_ResourcesXml" which you would set to null or "". The property would then construct the XmlDocument and set the backing field for the Resources Property. Similarly, reading _ResourcesXml could return Resources.OuterXml.

OTHER TIPS

I think you can achieve the requested result with something like this (using the MethodInvokingFactoryObject):

  <object id="Document" type="System.Xml.XmlDocument, System.Xml" />
  <object type="Spring.Objects.Factory.Config.MethodInvokingFactoryObject, Spring.Core">
    <property name="TargetObject">
      <ref local="Document" />
    </property>
    <property name="TargetMethod" value="AppendChild" />
    <property name="NamedArguments">
      <dictionary>
        <entry key="newChild">
          <object type="Spring.Objects.Factory.Config.MethodInvokingFactoryObject, Spring.Core">
            <property name="TargetObject">
              <ref local="Document" />
            </property>
            <property name="TargetMethod" value="CreateElement" />
            <property name="NamedArguments">
              <dictionary>
                <entry key="name" value="resources" />
              </dictionary>
            </property>
          </object>
        </entry>
      </dictionary>
    </property>
  </object>

Anyway, this seems very complex for the little effect you get. As Nader already mentioned it is a good idea to create a little helper factory.

Another option is to use expressions. You can call the methods within the config, e.g. as following:

<object id="..." type="..." expression="@(Document).CreateElement('resources')" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top