2009-04-08 2 views
1

XmlSerializer은 한 가지 예외를 제외하고 모든 것을 수행합니다. 그 요소의 속성으로 다른 요소와 요소를 쌍으로 연결해야합니다. 완전히 사용자 지정 serialize 메서드를 작성하고 싶지 않습니다. 다음은 클래스입니다.Xmlserializer - 컨트롤 요소 - 특성 페어링 (수정 된 버전)

public class Transaction 
{ 
    [XmlElement("ID")] 
    public int m_id; 

    [XmlElement("TransactionType")] 
    public string m_transactiontype; 

    [XmlAttribute("TransactionTypeCode")] 
    public string m_transactiontypecode; 
} 

다음과 같이 인스턴스화하고 일련 화합니다.

<Transaction TransactionTypeCode="520"> 
    <ID>1</ID> 
    <TransactionType>Withdrawal</TransactionType> 
    </Transaction> 

내가 원하는 :

<Transaction> 
    <ID>1</ID> 
    <TransactionType TransactionTypeCode="520">Withdrawal</TransactionType> 
    </Transaction> 

사람 (크리스 Dogget가) 제안 :

Transaction tx = new Transaction(); 

    tx.m_id = 1; 
    tx.m_transactiontype = "Withdrawal"; 
    tx.m_transactiontypecode = "520"; 

    StringWriter o = new 
    StringWriter(CultureInfo.InvariantCulture); 
    XmlSerializer s = new 
    XmlSerializer(typeof(Transaction)); 
    s.Serialize(o, tx); 
    Console.Write(o.ToString()); 

나 제공합니다

public class Transaction 
    { 

     [XmlElement("ID")] 
     public int m_id; 

     public TransactionType m_transactiontype; 
    } 

    public class TransactionType 
    { 
     public TransactionType(){} 
     public TransactionType(string type) { this.m_transactiontype = type; } 

     [XmlTextAttribute] 
     public string m_transactiontype; 

     [XmlAttribute("TransactionTypeCode")] 
     public string m_transactiontypecode; 
    } 

TransactionType 클래스의 사용은 약속 같은데 - 당신이 얼마나 위안 받았는지 보여 주실 수 있어요? 직렬화하기 전에 클래스를 인스턴스화합니까?

감사합니다!

+0

이 질문에 어떤 프로그래밍 언어로 태그를 붙이겠습니까? – Calvin

+0

질문을 조정해야 할 경우 질문을 수정하십시오. 새 것을 만들지 마라. 병합되었습니다. –

답변

0

Type 및 TypeCode 필드를 사용하여 별도의 클래스를 만들어야합니다. TypeCode를 [XmlAttribute]로 장식하고 [XmlText]와 함께 입력하십시오.

2
public class Transaction 
{ 

    [XmlElement("ID")] 
    public int m_id; 

    public TransactionType type; 
} 

public class TransactionType 
{ 
    public TransactionType(){} 
    public TransactionType(string type) { [email protected] = type; } 

    [XmlTextAttribute] 
    public string @Type; 

    [XmlAttribute("TypeCode")] 
    public string typecode; 
} 

나에게주는이 : 당신이 정말로 당신의 필드 이름으로 그 (대문자 이상) 사용할 수 있도록

<?xml version="1.0" encoding="utf-16"?> 
<Transaction xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <ID>1</ID> 
    <type TypeCode="520">Withdrawal</type> 
</Transaction> 

불행하게도, "유형", .NET의 실제 유형입니다.

+0

> ""유형 ".NET의 실제 유형, 당신 때문에 정말로 사용할 수는 없습니다. "- 물론 할 수 있습니다. 다른 네임 스페이스에 있으므로 상황이 정상적으로 처리됩니다. 다른 키워드와 일치하는 회원은 @ –

1

나는이 작업을 수행하는 유일한 방법은 멤버 자격 TypeCode를 사용하여 Type 클래스 (프레임 워크의 키워드이기도하지만 나쁜 이름 임)를 만드는 것이라고 믿습니다. 그런 다음 Transaction 클래스의 일부로 Type 인스턴스 또는 참조를가집니다.

0

IXmlSerializable을 구현하고 Linq to XML to construct your serialized object을 사용하십시오.

이렇게하면 XML의 모양을 정확하게 제어 할 수 있습니다.

개인적으로 XmlSerialization을 삭제하고 XAML로 가서 내 XML을 가져 오는 것을 잊어 버렸습니다. 으로 보입니다. 그것은 가 작동하는 한 허튼 소리처럼 보일 수 있습니다!

0

원하는 XML이 코드에서 개체를 레이아웃하는 방식과 일치하지 않습니다.

당신이 당신이 말하는 XML을 읽을 경우, 다음 ... 트랜잭션이 두 가지 요소가 포함되어

: ID 및 유형을. 유형에 TypeCode라는 속성이 있습니다.

생성하려고하는 대상을 보면 다음과 같이 말합니다.당신은 둘 사이의 분리를 볼 수 있습니다 ... ID, 종류 및 TypeCode를

:

거래는 세 가지 속성이 포함되어 있습니다. 적절한 직렬화 (클래스 또는 xml을 수정하여 통신하려는 대상이 서로 일치하도록)의 목표를 조정해야합니다.

0

OK,의는 제거 할 수 (내 불량) 문제 ''유형은 '닷넷의 유형입니다 "내가 인스턴스를 다음과 같이 직렬화

public class Transaction 
    { 
     [XmlElement("ID")] 
     public int m_id; 

     [XmlElement("TransactionType")] 
     public string m_transactiontype; 

     [XmlAttribute("TransactionTypeCode")] 
     public string m_transactiontypecode; 
    } 

;

<Transaction> 
    <ID>1</ID> 
    <TransactionType TransactionTypeCode="520">Withdrawal</TransactionType> 
</Transaction> 

TransactionType 클래스의 사용은 약속 같은데 - 당신은 당신이 직렬화하기 전에 클래스의 인스턴스를 얼마나 저를 보여줄 수 :

<Transaction TransactionTypeCode="520"> 
    <ID>1</ID> 
    <TransactionType>Withdrawal</TransactionType> 
</Transaction> 

내가 원하는 :

Transaction tx = new Transaction(); 

    tx.m_id = 1; 
    tx.m_transactiontype = "Withdrawal"; 
    tx.m_transactiontypecode = "520"; 

    StringWriter o = new StringWriter(CultureInfo.InvariantCulture); 
    XmlSerializer s = new XmlSerializer(typeof(Transaction)); 
    s.Serialize(o, tx); 
    Console.Write(o.ToString()); 

저를 준다?

감사합니다!

+0

GD가 내 게시물을 망쳤습니다. 새로운 질문을 올리면 ... –

1

당신의 유형이 코드를 가정 :

public class Transaction 
{ 
    public Transaction() { ttype = new TransactionType(); } 

    [XmlElement("ID")] 
    public int id; 

    [XmlElement("TransactionType")] 
    public TransactionType ttype; 
} 

public class TransactionType 
{ 
    public TransactionType(){} 
    public TransactionType(string txType) { this.type = txType; } 

    [XmlText] 
    public string type; 

    [XmlAttribute("TransactionTypeCode")] 
    public string typecode; 
} 

당신이 원하는 방식으로 초기화하고 직렬화합니다이 코드 :

public void Run() 
{ 
    XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); 
    ns.Add("", ""); 

    Transaction tx = new Transaction(); 
    tx.id = 1; 
    tx.ttype.type = "Withdrawal"; 
    tx.ttype.typecode = "520"; 
    using (StringWriter o = 
      new StringWriter(CultureInfo.InvariantCulture)) 
    { 
     XmlSerializer s = new XmlSerializer(typeof(Transaction)); 
     s.Serialize(o, tx, ns); 
     Console.Write(o.ToString()); 
    } 
}