3

I need to create an XML file using C#. I am using a class that inherits List that represents a list of computers and later initialize it with values but the serializer doesn't get the attributes for this class, only for its descendants. this is the class:

public class Computers : List<Computer>
    {
        [XmlAttribute("StorageType")]
        public int StorageType { get; set; }


        [XmlAttribute("StorageName")]
        public string StorageName { get; set; }            

    }

    public class Computer 
    {
        [XmlAttribute("StorageType")]
        public int StorageType { get; set; }

        [XmlAttribute("StorageName")]
        public string StorageName { get; set; }

        public string IPAddress { get; set; }
        public string Name { get; set; }
    }

The result should look something like this:

<fpc4:Computers StorageName="Computers" StorageType="1">
    <fpc4:Computer StorageName="{D37291CA-D1A7-4F34-87E4-8D84F1397BEA}" StorageType="1">
        <fpc4:IPAddress dt:dt="string">127.0.0.1</fpc4:IPAddress>
        <fpc4:Name dt:dt="string">Computer1</fpc4:Name>
    </fpc4:Computer>
    <fpc4:Computer StorageName="{AFE5707C-EA71-4442-9CA8-2A6264EAA814}" StorageType="1">
        <fpc4:IPAddress dt:dt="string">127.0.0.1</fpc4:IPAddress>
        <fpc4:Name dt:dt="string">Computer2</fpc4:Name>
    </fpc4:Computer>

But what I get so far is this:

<fpc4:Computers>
    <fpc4:Computer StorageType="1" StorageName="{7297fc09-3142-4284-b2e9-d6ea2fb1be78}">
      <fpc4:IPAddress>127.0.0.1</fpc4:IPAddress>
      <fpc4:Name>Computer1</fpc4:Name>
    </fpc4:Computer>
    <fpc4:Computer StorageType="1" StorageName="{eab517f6-aca9-4d01-a58b-143f2e3211e7}">
      <fpc4:IPAddress>127.0.0.1</fpc4:IPAddress>
      <fpc4:Name>Computer2</fpc4:Name>
    </fpc4:Computer>
  </fpc4:Computers>

As you can see the Computers node which is the parent node doesn't get the attributes.

Do you guys have a solution?


Assign Custom values to the $paginator->numbers in Cakephp Pagination

heres my situation, I have a user dashboard with tabs in it:

  1. 1st tab shows user projects that are active
  2. 2nd tab shows user projects that are expired.

along with a bunch of other tabs.

The problem started when I tried to paginate Tab1 and Tab2. I have the relative question over here. This question has been answered by petrocket although it does not work entirely.

The problem with the paginated links and counter remains.

when I do

<?php pr($this->Paginator->params['paging']); ?>

I get the following result:

Array (
    [UserProject] => Array
    (
        [page] => 1
        [current] => 1
        [count] => 8
        [prevPage] => 
        [nextPage] => 1
        [pageCount] => 8
        [order] => UserProject.id Desc
        [limit] => 1
        [options] => Array
            (
            )
        [paramType] => named
    )

    [UserProject2] => Array
    (
        [page] => 1
        [current] => 1
        [count] => 5
        [prevPage] => 
        [nextPage] => 1
        [pageCount] => 5
        [order] => UserProject2.id Desc
        [limit] => 1
        [options] => Array
            (
            )
        [paramType] => named
    )
)

My question is: Is there any way by which we can manually change the value of paginator counter, next, previous, first and last values?

My assumption is that if I can specify these values manually from the above array it will work fine. Right now it just takes the value of array [UserProject] correctly but not for array [UserProject2].

4

1 に答える 1

2

XmlSerializer葉ノードとは完全に分離されたリストを扱います。リストのプロパティは存在しません。含まれているデータの単なるコレクションです。より良いアプローチは次のとおりです。

public class Computers {
    private readonly List<Computer> items = new List<Computer>();
    [XmlElement("Computer")]
    public List<Computer> Items { get { return items; } }

    [XmlAttribute("StorageType")]
    public int StorageType { get; set; }

    [XmlAttribute("StorageName")]
    public string StorageName { get; set; }   
}

これは、一連のコンピューターと2 つの属性持つオブジェクトですが、リスト自体ではありません。リストに for を使用すると、必要に応じてネストが平坦化されます。便宜上、名前空間を省略していることに注意してください。XmlElementAttribute

リストからの継承 (メンバーの追加を目的とした) は、 だけでなくXmlSerlaizer、さまざまなシリアライザーやバインディング フレームワークでもうまく機能しません。

于 2013-02-11T12:35:21.443 に答える