클래스를 XML로 직렬화하고 속성을 중첩 노드가 아닌 클래스의 특성으로 직렬화하려고합니다. WebApi를 사용하여 XML의 직렬화를 자동으로 처리하고 있습니다.
이것은 내 수업입니다.
[DataContract (Namespace="", Name="AttributeTest")]
[Serializable]
public class AttributeTestClass
{
[XmlAttribute("Property")]
[DataMember]
public int Property1 { get; set; }
}
내가 받고있는 출력은 다음과 같습니다 ( 으로 장식 되었음에도 불구하고 속성 Property1
이 아닙니다[XmlAttribute]
).
<AttributeTest xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Property1>123</Property1>
</AttributeTest>
이것은 내가 받고 싶은 출력입니다.
<AttributeTest Property1="123" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
</AttributeTest>
내가 무엇을 놓치고 있습니까?
나는 WebApi에 익숙하지 않지만 수신되는 출력 은 필요 DataContractSerializer
하지 않은 XmlSerializer
을 사용하여 직렬화 된 것처럼 보입니다 . 에 다음을 추가하는 경우 확인 Application_Start
에 Global.asax
도움이 :
GlobalConfiguration.Configuration.Formatters.Clear();
GlobalConfiguration.Configuration.Formatters.Add(
new System.Net.Http.Formatting.XmlMediaTypeFormatter());
GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true;
( http://serena-yeoh.blogspot.de/2013/02/xml-serialization-in-aspnet-web-api.html에서 )
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다