开发者

DataSet Merge without Schema will never override data, right?

I am working with existing code and hoping to change data without recompiling 开发者_如何学JAVA(as such this could be seen as a SuperUser issue).

We currently DataSet.ReadXml from a (compiled) resources-based XML file. Then we check for a local XML file and, if present, DataSet.ReadXml that file and then DataSet.Merge the result into the original.

Seeing as we have no schema in the compiled resources-based XML, there is no way for the external XML to override any data, only add to it, yes?


My question title may have seemed to preclude this option, but it wasn't intended:

You can get the local file's data to override the compiled file, by including the xs:schema as the first sub-element of the XML.

I.e. when the original data has no schema, the merged schema will be used.

E.g.

Original:

<NewDataSet>
  <dt1>
    <k>1</k>
    <l>test</l>
    <m>1</m>
  </dt1>
  <dt1>
    <k>2</k>
    <l>test</l>
    <m>2</m>
  </dt1>
  <dt1>
    <k>2</k>
    <l>tst</l>
    <m>3</m>
  </dt1>
</NewDataSet>

New:

<NewDataSet>
 <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="dt1">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="k" type="xs:string" />
              <xs:element name="l" type="xs:string" />
              <xs:element name="m" type="xs:string" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
    <xs:unique name="Constraint1" msdata:PrimaryKey="true">
      <xs:selector xpath=".//dt1" />
      <xs:field xpath="k" />
      <xs:field xpath="l" />
    </xs:unique>
  </xs:element>
 </xs:schema>
  <dt1>
    <k>1</k>
    <l>test</l>
    <m>1</m>
  </dt1>
  <dt1>
    <k>2</k>
    <l>test</l>
    <m>3</m>
  </dt1>
  <dt1>
    <k>3</k>
    <l>test</l>
    <m>4</m>
  </dt1>
</NewDataSet>

Merged (for simplicity I include the WriteXml output):

<NewDataSet>
  <dt1>
    <k>1</k>
    <l>test</l>
    <m>1</m>
  </dt1>
  <dt1>
    <k>2</k>
    <l>test</l>
    <m>3</m>
  </dt1>
  <dt1>
    <k>2</k>
    <l>tst</l>
    <m>3</m>
  </dt1>
  <dt1>
    <k>3</k>
    <l>test</l>
    <m>4</m>
  </dt1>
</NewDataSet>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜