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 클래스의 사용은 약속 같은데 - 당신이 얼마나 위안 받았는지 보여 주실 수 있어요? 직렬화하기 전에 클래스를 인스턴스화합니까?
감사합니다!
이 질문에 어떤 프로그래밍 언어로 태그를 붙이겠습니까? – Calvin
질문을 조정해야 할 경우 질문을 수정하십시오. 새 것을 만들지 마라. 병합되었습니다. –