10

シリアライズ時にリストプロパティタグを避けることは可能ですか?

//[Serializable()] - removed, unnecessary
public class Foo
{
    protected List<FooBar> fooBars = new List<FooBar>();
    public virtual List<FooBar> FooBars
    {
        get { return fooBars; }
        set { fooBars = value; }
    }
}

// [Serializable()] - removed, unnecessary
public class FooBar
{
    public int MyProperty
    { get; set; }
}

Foo をシリアライズすると (コメントを除く):

<Foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <FooBars>    <!-- Unwanted tag -->
    <FooBar>
      <MyProperty>7</MyProperty> 
    </FooBar>
    <FooBar>
      <MyProperty>9</MyProperty> 
    </FooBar>
  </FooBars>
</Foo>

必要な出力:

<Foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <FooBar>
    <MyProperty>7</MyProperty> 
  </FooBar>
  <FooBar>
    <MyProperty>9</MyProperty> 
  </FooBar>

4

1 に答える 1

12

追加:

[System.Xml.Serialization.XmlElement("FooBar")]
public virtual List<FooBar> FooBars 
{ 
    get { return fooBars; } 
    set { fooBars = value; }
}

結果は

<FooMain xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http:/
/www.w3.org/2001/XMLSchema">
  <FooBar>
    <MyProperty>7</MyProperty>
  </FooBar>
  <FooBar>
    <MyProperty>76</MyProperty>
  </FooBar>
  <FooBar>
    <MyProperty>67</MyProperty>
  </FooBar>
</FooMain>
于 2008-11-24T14:48:32.397 に答える