CLR 유형과 xsd 유형 코드간에 표준 (프레임 워크) 맵핑이 있습니까? 문자열, 정수, 10 진수 등을 해당하는 XmlSchemaSimpleType으로 변환해야합니다.System.Type에서 xs : type으로 유형 맵
필자는 필요한 간단한 유형을 구성하고 직접 매핑을 수행하는 case 문을 사용할 수 있습니다. 나는 그들의 CLR 타입들로부터 XmlSchemaSimpleType을 만들거나 심지어 CLR 타입에서 XmlTypeCode로 매핑 할 수도있는 표준 프레임 워크 클래스가되기를 바랬다.
선택 System.String -> XmlTypeCode.String
감사
(예를 들어)UPDATE (2010년 7월 7일는) 감사합니다, 나는 읽고 조금 조정이 필요 링크 - 다른 사람을 위해 linqpad에 붙여 넣을 수있는 마지막 코드는 다음과 같습니다.
public class XmlValueWrapper
{
public object Value { get; set; }
}
public static class XsdConvert
{
private static XmlSerializer serializer = new XmlSerializer(typeof(XmlValueWrapper));
public static object ConvertFrom(string value, string xsdType)
{
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
XNamespace xsd = "http://www.w3.org/2001/XMLSchema";
XDocument doc = new XDocument(
new XElement("XmlValueWrapper",
new XAttribute(XNamespace.Xmlns + "xsi", xsi),
new XAttribute(XNamespace.Xmlns + "xs", xsd),
new XElement("Value",
new XAttribute(xsi + "type", xsdType),
new XText(value))
)
);
doc.Dump("try");
using (var reader = doc.CreateReader()) {
XmlValueWrapper wrapper = (XmlValueWrapper) serializer.Deserialize(reader);
wrapper.Dump("ITEM");
return wrapper.Value;
}
}
}
public static void Main()
{
object o = XsdConvert.ConvertFrom("2010-01-02", "xs:date");
o.GetType().Dump("object");
/*
Debug.Assert(Equals(42, XsdConverta.ConvertFrom("42", "xsd:int")));
Debug.Assert(Equals(42.0, XsdConverta.ConvertFrom("42", "xsd:double")));
Debug.Assert(Equals(42m, XsdConverta.ConvertFrom("42", "xsd:decimal")));
Debug.Assert(Equals("42", XsdConverta.ConvertFrom("42", "xsd:string")));
Debug.Assert(Equals(true, XsdConverta.ConvertFrom("true", "xsd:boolean")));
Debug.Assert(Equals(new DateTime(2009, 4, 17), XsdConverta.ConvertFrom("2009-04-17", "xsd:date")));*/
}