本文整理汇总了C#中System.Xml.Serialization.ClassMap.GetElement方法的典型用法代码示例。如果您正苦于以下问题:C# ClassMap.GetElement方法的具体用法?C# ClassMap.GetElement怎么用?C# ClassMap.GetElement使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Xml.Serialization.ClassMap
的用法示例。
在下文中一共展示了ClassMap.GetElement方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ImportChoiceContent
void ImportChoiceContent (XmlQualifiedName typeQName, ClassMap cmap, XmlSchemaChoice choice, CodeIdentifiers classIds, bool multiValue)
{
XmlTypeMapElementInfoList choices = new XmlTypeMapElementInfoList ();
multiValue = ImportChoices (typeQName, null, choices, choice.Items) || multiValue;
if (choices.Count == 0) return;
if (choice.MaxOccurs > 1) multiValue = true;
XmlTypeMapMemberElement member;
if (multiValue)
{
member = new XmlTypeMapMemberFlatList ();
member.Name = classIds.AddUnique ("Items", member);
ListMap listMap = new ListMap ();
listMap.ItemInfo = choices;
((XmlTypeMapMemberFlatList)member).ListMap = listMap;
}
else
{
member = new XmlTypeMapMemberElement ();
member.Name = classIds.AddUnique ("Item", member);
}
// If all choices have the same type, use that type for the member.
// If not use System.Object.
// If there are at least two choices with the same type, use a choice
// identifier attribute
TypeData typeData = null;
bool twoEqual = false;
bool allEqual = true;
Hashtable types = new Hashtable ();
for (int n = choices.Count - 1; n >= 0; n--)
{
XmlTypeMapElementInfo einfo = (XmlTypeMapElementInfo) choices [n];
// In some complex schemas, we may end up with several options
// with the same name. It is better to ignore the extra options
// than to crash. It's the best we can do, and btw it works
// better than in MS.NET.
if (cmap.GetElement (einfo.ElementName, einfo.Namespace, einfo.ExplicitOrder) != null ||
choices.IndexOfElement (einfo.ElementName, einfo.Namespace) != n)
{
choices.RemoveAt (n);
continue;
}
if (types.ContainsKey (einfo.TypeData)) twoEqual = true;
else types.Add (einfo.TypeData, einfo);
TypeData choiceType = einfo.TypeData;
if (choiceType.SchemaType == SchemaTypes.Class)
{
// When comparing class types, use the most generic class in the
// inheritance hierarchy
XmlTypeMapping choiceMap = GetTypeMapping (choiceType);
BuildPendingMap (choiceMap);
while (choiceMap.BaseMap != null) {
choiceMap = choiceMap.BaseMap;
BuildPendingMap (choiceMap);
choiceType = choiceMap.TypeData;
}
}
if (typeData == null) typeData = choiceType;
else if (typeData != choiceType) allEqual = false;
}
if (!allEqual)
typeData = TypeTranslator.GetTypeData (typeof(object));
if (twoEqual)
{
// Create the choice member
XmlTypeMapMemberElement choiceMember = new XmlTypeMapMemberElement ();
choiceMember.Ignore = true;
choiceMember.Name = classIds.AddUnique (member.Name + "ElementName", choiceMember);
member.ChoiceMember = choiceMember.Name;
// Create the choice enum
XmlTypeMapping enumMap = CreateTypeMapping (new XmlQualifiedName (member.Name + "ChoiceType", typeQName.Namespace), SchemaTypes.Enum, null);
enumMap.IncludeInSchema = false;
CodeIdentifiers codeIdents = new CodeIdentifiers ();
EnumMap.EnumMapMember[] members = new EnumMap.EnumMapMember [choices.Count];
for (int n=0; n<choices.Count; n++)
{
XmlTypeMapElementInfo it =(XmlTypeMapElementInfo) choices[n];
bool extraNs = (it.Namespace != null && it.Namespace != "" && it.Namespace != typeQName.Namespace);
string xmlName = extraNs ? it.Namespace + ":" + it.ElementName : it.ElementName;
string enumName = codeIdents.AddUnique (CodeIdentifier.MakeValid (it.ElementName), it);
members [n] = new EnumMap.EnumMapMember (xmlName, enumName);
}
enumMap.ObjectMap = new EnumMap (members, false);
choiceMember.TypeData = multiValue ? enumMap.TypeData.ListTypeData : enumMap.TypeData;
choiceMember.ElementInfo.Add (CreateElementInfo (typeQName.Namespace, choiceMember, choiceMember.Name, choiceMember.TypeData, false, XmlSchemaForm.None, -1));
//.........这里部分代码省略.........
示例2: GenerateReadMembers
//.........这里部分代码省略.........
WriteLine ("");
}
if (_format == SerializationFormat.Encoded && map.ElementMembers != null)
{
_fixupCallbacks.Add (xmlMap);
WriteLine ("Fixup fixup = new Fixup(" + ob + ", new XmlSerializationFixupCallback(" + GetFixupCallbackName (xmlMap) + "), " + map.ElementMembers.Count + ");");
WriteLine ("AddFixup (fixup);");
WriteLine ("");
}
ArrayList infos = null;
int maxInd;
if (readBySoapOrder) {
if (map.ElementMembers != null) maxInd = map.ElementMembers.Count;
else maxInd = 0;
}
else
{
infos = new ArrayList ();
infos.AddRange (map.AllElementInfos);
maxInd = infos.Count;
WriteLine ("while (Reader.NodeType != System.Xml.XmlNodeType.EndElement) ");
WriteLineInd ("{");
WriteLine ("if (Reader.NodeType == System.Xml.XmlNodeType.Element) ");
WriteLineInd ("{");
}
first = true;
for (int ind = 0; ind < maxInd; ind++)
{
XmlTypeMapElementInfo info = readBySoapOrder ? map.GetElement (ind) : (XmlTypeMapElementInfo) infos [ind];
if (!readBySoapOrder)
{
if (info.IsTextElement || info.IsUnnamedAnyElement) continue;
string elemCond = first ? "" : "else ";
elemCond += "if (";
if (info.ExplicitOrder >= 0)
elemCond += "idx < " + info.ExplicitOrder + "&& ";
if (!(info.Member.IsReturnValue && _format == SerializationFormat.Encoded)) {
elemCond += "Reader.LocalName == " + GetLiteral (info.ElementName);
if (!map.IgnoreMemberNamespace) elemCond += " && Reader.NamespaceURI == " + GetLiteral (info.Namespace);
elemCond += " && ";
}
if (readFlag[info.Member.Index] != null)
elemCond += "!" + readFlag[info.Member.Index] + ") {";
else
elemCond += "true) {";
WriteLineInd (elemCond);
}
if (info.Member.GetType() == typeof (XmlTypeMapMemberList))
{
if (_format == SerializationFormat.Encoded && info.MultiReferenceType)
{
string list = GetObTempVar ();
WriteLine ("object " + list + " = ReadReferencingElement (out fixup.Ids[" + info.Member.Index + "]);");
RegisterReferencingMap (info.MappedType);
WriteLineInd ("if (fixup.Ids[" + info.Member.Index + "] == null) {"); // Already read
if (IsReadOnly (typeMap, info.Member, info.TypeData, isValueList))
WriteLine ("throw CreateReadOnlyCollectionException (" + GetLiteral(info.TypeData.CSharpFullName) + ");");
else
示例3: ReadMembers
void ReadMembers (ClassMap map, object ob, bool isValueList, bool readByOrder)
{
// Set the default values of the members
if (map.MembersWithDefault != null)
{
ArrayList members = map.MembersWithDefault;
for (int n=0; n<members.Count; n++) {
XmlTypeMapMember mem = (XmlTypeMapMember) members[n];
SetMemberValueFromAttr (mem, ob, mem.DefaultValue, isValueList);
}
}
// Reads attributes
XmlTypeMapMember anyAttrMember = map.DefaultAnyAttributeMember;
int anyAttributeIndex = 0;
object anyAttributeArray = null;
while (Reader.MoveToNextAttribute())
{
XmlTypeMapMemberAttribute member = map.GetAttribute (Reader.LocalName, Reader.NamespaceURI);
if (member != null)
{
SetMemberValue (member, ob, GetValueFromXmlString (Reader.Value, member.TypeData, member.MappedType), isValueList);
}
else if (IsXmlnsAttribute(Reader.Name))
{
// If the map has NamespaceDeclarations,
// then store this xmlns to the given member.
// If the instance doesn't exist, then create.
if (map.NamespaceDeclarations != null) {
XmlSerializerNamespaces nss = this.GetMemberValue (map.NamespaceDeclarations, ob, isValueList) as XmlSerializerNamespaces;
if (nss == null) {
nss = new XmlSerializerNamespaces ();
SetMemberValue (map.NamespaceDeclarations, ob, nss, isValueList);
}
if (Reader.Prefix == "xmlns")
nss.Add (Reader.LocalName, Reader.Value);
else
nss.Add ("", Reader.Value);
}
}
else if (anyAttrMember != null)
{
XmlAttribute attr = (XmlAttribute) Document.ReadNode(Reader);
ParseWsdlArrayType (attr);
AddListValue (anyAttrMember.TypeData, ref anyAttributeArray, anyAttributeIndex++, attr, true);
}
else
ProcessUnknownAttribute(ob);
}
if (anyAttrMember != null)
{
anyAttributeArray = ShrinkArray ((Array)anyAttributeArray, anyAttributeIndex, anyAttrMember.TypeData.Type.GetElementType(), true);
SetMemberValue (anyAttrMember, ob, anyAttributeArray, isValueList);
}
if (!isValueList)
{
Reader.MoveToElement();
if (Reader.IsEmptyElement)
return;
Reader.ReadStartElement();
}
// Reads elements
bool[] readFlag = new bool[(map.ElementMembers != null) ? map.ElementMembers.Count : 0];
bool hasAnyReturnMember = (isValueList && _format == SerializationFormat.Encoded && map.ReturnMember != null
&& map.ReturnMember.TypeData.Type == typeof(object));
Reader.MoveToContent();
int[] indexes = null;
object[] flatLists = null;
Fixup fixup = null;
int ind = 0;
int maxInd;
if (readByOrder) {
if (map.ElementMembers != null) maxInd = map.ElementMembers.Count;
else maxInd = 0;
}
else
maxInd = int.MaxValue;
if (map.FlatLists != null)
{
indexes = new int[map.FlatLists.Count];
flatLists = new object[map.FlatLists.Count];
foreach (XmlTypeMapMemberExpandable mem in map.FlatLists)
if (IsReadOnly (mem, ob, isValueList)) flatLists[mem.FlatArrayIndex] = mem.GetValue (ob);
}
if (_format == SerializationFormat.Encoded && map.ElementMembers != null)
{
//.........这里部分代码省略.........
示例4: ReadMembers
void ReadMembers (ClassMap map, object ob, bool isValueList, bool readByOrder)
{
// Reads attributes
ReadAttributeMembers (map, ob, isValueList);
if (!isValueList)
{
Reader.MoveToElement();
if (Reader.IsEmptyElement) {
SetListMembersDefaults (map, ob, isValueList);
return;
}
Reader.ReadStartElement();
}
// Reads elements
bool[] readFlag = new bool[(map.ElementMembers != null) ? map.ElementMembers.Count : 0];
bool hasAnyReturnMember = (isValueList && _format == SerializationFormat.Encoded && map.ReturnMember != null);
Reader.MoveToContent();
int[] indexes = null;
object[] flatLists = null;
object[] flatListsChoices = null;
Fixup fixup = null;
int ind = 0;
int maxInd;
if (readByOrder) {
if (map.ElementMembers != null) maxInd = map.ElementMembers.Count;
else maxInd = 0;
}
else
maxInd = int.MaxValue;
if (map.FlatLists != null)
{
indexes = new int[map.FlatLists.Count];
flatLists = new object[map.FlatLists.Count];
foreach (XmlTypeMapMemberExpandable mem in map.FlatLists) {
if (IsReadOnly (mem, mem.TypeData, ob, isValueList))
flatLists [mem.FlatArrayIndex] = mem.GetValue (ob);
else if (mem.TypeData.Type.IsArray) {
flatLists [mem.FlatArrayIndex] = InitializeList (mem.TypeData);
}
else {
object list = mem.GetValue (ob);
if (list == null) {
list = InitializeList (mem.TypeData);
SetMemberValue (mem, ob, list, isValueList);
}
flatLists [mem.FlatArrayIndex] = list;
}
if (mem.ChoiceMember != null) {
if (flatListsChoices == null)
flatListsChoices = new object [map.FlatLists.Count];
flatListsChoices [mem.FlatArrayIndex] = InitializeList (mem.ChoiceTypeData);
}
}
}
if (_format == SerializationFormat.Encoded && map.ElementMembers != null)
{
FixupCallbackInfo info = new FixupCallbackInfo (this, map, isValueList);
fixup = new Fixup(ob, new XmlSerializationFixupCallback(info.FixupMembers), map.ElementMembers.Count);
AddFixup (fixup);
}
while (Reader.NodeType != System.Xml.XmlNodeType.EndElement && (ind < maxInd))
{
if (Reader.NodeType == System.Xml.XmlNodeType.Element)
{
XmlTypeMapElementInfo info;
if (readByOrder) {
info = map.GetElement (ind++);
}
else if (hasAnyReturnMember) {
info = (XmlTypeMapElementInfo) ((XmlTypeMapMemberElement)map.ReturnMember).ElementInfo[0];
hasAnyReturnMember = false;
}
else
info = map.GetElement (Reader.LocalName, Reader.NamespaceURI);
if (info != null && !readFlag[info.Member.Index] )
{
if (info.Member.GetType() == typeof (XmlTypeMapMemberList))
{
if (_format == SerializationFormat.Encoded && info.MultiReferenceType)
{
object list = ReadReferencingElement (out fixup.Ids[info.Member.Index]);
if (fixup.Ids[info.Member.Index] == null) // Already read
{
if (IsReadOnly (info.Member, info.TypeData, ob, isValueList)) throw CreateReadOnlyCollectionException (info.TypeData.FullTypeName);
else SetMemberValue (info.Member, ob, list, isValueList);
}
//.........这里部分代码省略.........
示例5: GenerateReadMembers
void GenerateReadMembers (XmlMapping xmlMap, ClassMap map, string ob, bool isValueList, bool readByOrder)
{
XmlTypeMapping typeMap = xmlMap as XmlTypeMapping;
Type xmlMapType = (typeMap != null) ? typeMap.TypeData.Type : typeof(object[]);
// Load the default value of members
if (map.MembersWithDefault != null)
{
ArrayList members = map.MembersWithDefault;
for (int n=0; n<members.Count; n++) {
XmlTypeMapMember mem = (XmlTypeMapMember) members[n];
GenerateSetMemberValueFromAttr (mem, ob, GetLiteral (mem.DefaultValue), isValueList);
}
}
// A value list cannot have attributes
bool first;
if (!isValueList && !GenerateReadHook (HookType.attributes, xmlMapType))
{
// Reads attributes
XmlTypeMapMember anyAttrMember = map.DefaultAnyAttributeMember;
if (anyAttrMember != null)
{
WriteLine ("int anyAttributeIndex = 0;");
WriteLine (anyAttrMember.TypeData.FullTypeName + " anyAttributeArray = null;");
}
WriteLine ("while (Reader.MoveToNextAttribute())");
WriteLineInd ("{");
first = true;
if (map.AttributeMembers != null) {
foreach (XmlTypeMapMemberAttribute at in map.AttributeMembers)
{
WriteLineInd ((first?"":"else ") + "if (Reader.LocalName == " + GetLiteral (at.AttributeName) + " && Reader.NamespaceURI == " + GetLiteral (at.Namespace) + ") {");
if (!GenerateReadMemberHook (xmlMapType, at)) {
GenerateSetMemberValue (at, ob, GenerateGetValueFromXmlString ("Reader.Value", at.TypeData, at.MappedType), isValueList);
GenerateEndHook ();
}
WriteLineUni ("}");
first = false;
}
}
WriteLineInd ((first?"":"else ") + "if (IsXmlnsAttribute (Reader.Name)) {");
// If the map has NamespaceDeclarations,
// then store this xmlns to the given member.
// If the instance doesn't exist, then create.
if (map.NamespaceDeclarations != null) {
if (!GenerateReadMemberHook (xmlMapType, map.NamespaceDeclarations)) {
string nss = ob + "[email protected]" + map.NamespaceDeclarations.Name;
WriteLine ("if (" + nss + " == null) " + nss + " = new XmlSerializerNamespaces ();");
WriteLineInd ("if (Reader.Prefix == \"xmlns\")");
WriteLine (nss + ".Add (Reader.LocalName, Reader.Value);");
Unindent ();
WriteLineInd ("else");
WriteLine (nss + ".Add (\"\", Reader.Value);");
Unindent ();
GenerateEndHook ();
}
}
WriteLineUni ("}");
WriteLineInd ("else {");
if (anyAttrMember != null)
{
if (!GenerateReadArrayMemberHook (xmlMapType, anyAttrMember, "anyAttributeIndex")) {
WriteLine ("System.Xml.XmlAttribute attr = (System.Xml.XmlAttribute) Document.ReadNode(Reader);");
if (typeof(System.Xml.Schema.XmlSchemaAnnotated).IsAssignableFrom (xmlMapType))
WriteLine ("ParseWsdlArrayType (attr);");
GenerateAddListValue (anyAttrMember.TypeData, "anyAttributeArray", "anyAttributeIndex", GetCast (anyAttrMember.TypeData.ListItemTypeData, "attr"), true);
GenerateEndHook ();
}
WriteLine ("anyAttributeIndex++;");
}
else {
if (!GenerateReadHook (HookType.unknownAttribute, xmlMapType)) {
WriteLine ("UnknownNode (" + ob + ");");
GenerateEndHook ();
}
}
WriteLineUni ("}");
WriteLineUni ("}");
if (anyAttrMember != null && !MemberHasReadReplaceHook (xmlMapType, anyAttrMember))
{
WriteLine ("");
WriteLine("anyAttributeArray = (" + anyAttrMember.TypeData.FullTypeName + ") ShrinkArray (anyAttributeArray, anyAttributeIndex, " + GetTypeOf(anyAttrMember.TypeData.Type.GetElementType()) + ", true);");
GenerateSetMemberValue (anyAttrMember, ob, "anyAttributeArray", isValueList);
}
WriteLine ("");
GenerateEndHook ();
}
//.........这里部分代码省略.........