本文整理汇总了C#中System.Xml.Serialization.XmlElementAttribute类的典型用法代码示例。如果您正苦于以下问题:C# XmlElementAttribute类的具体用法?C# XmlElementAttribute怎么用?C# XmlElementAttribute使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
XmlElementAttribute类属于System.Xml.Serialization命名空间,在下文中一共展示了XmlElementAttribute类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ElementNameDefault
public void ElementNameDefault ()
{
XmlElementAttribute attr = new XmlElementAttribute ();
Assert.AreEqual (string.Empty, attr.ElementName, "#1");
attr.ElementName = null;
Assert.AreEqual (string.Empty, attr.ElementName, "#2");
}
示例2: Insert
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public void Insert(int index, XmlElementAttribute value)
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
_list.Insert(index, value);
}
示例3: Add
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public int Add(XmlElementAttribute value)
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
int index = _list.Count;
_list.Add(value);
return index;
}
示例4: Remove
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public void Remove(XmlElementAttribute value)
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
if (!_list.Remove(value))
{
throw new ArgumentException(SR.Arg_RemoveArgNotFound);
}
}
示例5: DifferentItemTypes
public void DifferentItemTypes()
{
XmlElementAttribute element1 = new XmlElementAttribute("myname", typeof(SerializeMe));
XmlElementAttribute element2 = new XmlElementAttribute("myname", typeof(SerializeMeToo));
atts1.XmlElements.Add(element1);
atts2.XmlElements.Add(element2);
ov1.Add(typeof(SerializeMe), atts1);
ov2.Add(typeof(SerializeMe), atts2);
ThumbprintHelpers.DifferentThumbprint(ov1, ov2);
}
示例6: SameDataType
public void SameDataType()
{
XmlElementAttribute element1 = new XmlElementAttribute("myname", typeof(SerializeMe));
element1.DataType = "myfirstxmltype";
XmlElementAttribute element2 = new XmlElementAttribute("myname", typeof(SerializeMe));
element2.DataType = "myfirstxmltype";
atts1.XmlElements.Add(element1);
atts2.XmlElements.Add(element2);
ov1.Add(typeof(SerializeMe), atts1);
ov2.Add(typeof(SerializeMe), atts2);
ThumbprintHelpers.SameThumbprint(ov1, ov2);
}
示例7: AddSimpleXMLElement
private static void AddSimpleXMLElement(FileLevelInfo info, XmlElementAttribute xea, ClassSnippet snip, FieldInfo finfo)
{
string fieldName = xea.ElementName;
string dtype = SetupSimpleType(info, snip, xea.DataType, fieldName, finfo.FieldType.Name);
string nodeGetter = "pNode->FirstChildElement(\"" + fieldName + "\")";
snip.fromXml += "\t" + fieldName + " = FromString_" + dtype + "(pNode, GetValue(\"" + fieldName + "\", pNode->FirstChildElement(\"" + fieldName + "\"), pNode));\n";
snip.toXml += "\tTiXmlElement* n" + fieldName + " = new TiXmlElement(\"" + fieldName+ "\");\n";
snip.toXml += "\tTiXmlText* nt" + fieldName + " = new TiXmlText(ToString_" + dtype + "(" + fieldName + "));\n";
snip.toXml += "\tn" + fieldName + "->LinkEndChild(nt"+fieldName+");\n";
snip.toXml += "\tpEm->LinkEndChild(n" + fieldName + ");\n";
snip.declarations += "\t" + dtype + " " + fieldName + ";\n";
}
示例8: VewerkData
/// <summary>
/// verwerk de data in het project object tot XML file
/// </summary>
/// <param name="proj">te verwerken tot XML file</param>
/// <param name="newFile">moet er een nieuw bestand gemaakt worden</param>
/// <returns>succes boolean</returns>
public bool VewerkData(Project proj, bool newFile)
{
if (newFile)
{
NewNameFile();
}
filename = "result" + count.ToString() + ".xml";
// elk overridden field, property, of type heeft een XmlAttributes instance nodig.
XmlAttributes xmlAttrs = new XmlAttributes();
// maken van het attribuut
XmlElementAttribute attr = new XmlElementAttribute();
attr.ElementName = "newVogel";
attr.Type = typeof(Vogel);
xmlAttrs.XmlElements.Add(attr);
// maken van het override attribuut
XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides();
// voeg de override waarde toe aan de lijst met het veld dat de inheritance heeft samen met de attribuuten
attrOverrides.Add(typeof(Waarneming), "Dier", xmlAttrs);
try
{
Type t = typeof(Code_Layer.Project);
XmlSerializer xmls = new XmlSerializer(t, attrOverrides);
using (FileStream fs = new FileStream(XMLPath + filename, FileMode.Create))
{
xmls.Serialize(fs, proj);
}
}
catch (Exception e)//error afhandelen
{
Console.WriteLine(e.Message);
Console.WriteLine(e.InnerException);
Console.WriteLine(e.StackTrace);
return false;
}
return true;
}
示例9: DeserializeLocations
/// <summary>
/// Загрузка дерева локаций из XML-файла.
/// </summary>
/// <param name="fileName">Имя файла.</param>
/// <returns>Локация первого уровня.</returns>
public static Location DeserializeLocations(string fileName)
{
XmlAttributes attrs = new XmlAttributes();
XmlElementAttribute attr = new XmlElementAttribute();
attr.ElementName = "Location";
attr.Type = typeof(Location);
attrs.XmlElements.Add(attr);
XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides();
attrOverrides.Add(typeof(Location), "Instruments", attrs);
XmlSerializer writter = new XmlSerializer(typeof(Location), attrOverrides);
var path = fileName;
FileStream file = new FileStream(path, FileMode.Open);
Location root = (Location)writter.Deserialize(file);
file.Close();
return root;
}
示例10: DeserializeObject
public void DeserializeObject(string filename)
{
XmlAttributeOverrides attrOverrides =
new XmlAttributeOverrides();
XmlAttributes attrs = new XmlAttributes();
// Create an XmlElementAttribute to override the Instrument.
XmlElementAttribute attr = new XmlElementAttribute();
attr.ElementName = "Brass";
attr.Type = typeof(Brass);
// Add the XmlElementAttribute to the collection of objects.
attrs.XmlElements.Add(attr);
attrOverrides.Add(typeof(Orchestra), "Instruments", attrs);
// Create the XmlSerializer using the XmlAttributeOverrides.
XmlSerializer s =
new XmlSerializer(typeof(Orchestra), attrOverrides);
FileStream fs = new FileStream(filename, FileMode.Open);
Orchestra band = (Orchestra)s.Deserialize(fs);
Console.WriteLine("Brass:");
/* The difference between deserializing the overridden
XML document and serializing it is this: To read the derived
object values, you must declare an object of the derived type
(Brass), and cast the Instrument instance to it. */
Brass b;
foreach (Instrument i in band.Instruments)
{
b = (Brass)i;
Console.WriteLine(
b.Name + "\n" +
b.IsValved);
}
}
示例11: TestSerializeCollectionWithXmlElementAttribute
public void TestSerializeCollectionWithXmlElementAttribute()
{
// the rule is:
// if no type is specified or the specified type
// matches the contents of the collection,
// serialize each element in an element named after the member.
// if the type does not match, or matches the collection itself,
// create a base wrapping element for the member, and then
// wrap each collection item in its own wrapping element based on type.
XmlAttributeOverrides overrides = new XmlAttributeOverrides();
XmlAttributes attr = new XmlAttributes();
XmlElementAttribute element = new XmlElementAttribute();
attr.XmlElements.Add(element);
overrides.Add(typeof(StringCollectionContainer), "Messages", attr);
// empty collection & no type info in XmlElementAttribute
StringCollectionContainer container = new StringCollectionContainer();
Serialize(container, overrides);
AssertEquals(Infoset("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
// non-empty collection & no type info in XmlElementAttribute
container.Messages.Add("hello");
Serialize(container, overrides);
AssertEquals (Infoset("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages>hello</Messages></StringCollectionContainer>"), WriterText);
// non-empty collection & only type info in XmlElementAttribute
element.Type = typeof(StringCollection);
Serialize(container, overrides);
AssertEquals (Infoset("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages><string>hello</string></Messages></StringCollectionContainer>"), WriterText);
// non-empty collection & only type info in XmlElementAttribute
element.Type = typeof(string);
Serialize(container, overrides);
AssertEquals(Infoset("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages>hello</Messages></StringCollectionContainer>"), WriterText);
// two elements
container.Messages.Add("goodbye");
element.Type = null;
Serialize(container, overrides);
AssertEquals(Infoset("<StringCollectionContainer xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Messages>hello</Messages><Messages>goodbye</Messages></StringCollectionContainer>"), WriterText);
}
示例12: BuildHeadersReflectionMembers
XmlReflectionMember [] BuildHeadersReflectionMembers (HeaderInfo[] headers)
{
XmlReflectionMember [] mems = new XmlReflectionMember [headers.Length];
for (int n=0; n<headers.Length; n++)
{
HeaderInfo header = headers [n];
XmlReflectionMember m = new XmlReflectionMember ();
m.IsReturnValue = false;
m.MemberName = header.HeaderType.Name;
m.MemberType = header.HeaderType;
// MS.NET reflects header classes in a weird way. The root element
// name is the CLR class name unless it is specified in an XmlRootAttribute.
// The usual is to use the xml type name by default, but not in this case.
XmlAttributes ats = new XmlAttributes (header.HeaderType);
if (ats.XmlRoot != null) {
XmlElementAttribute xe = new XmlElementAttribute ();
xe.ElementName = ats.XmlRoot.ElementName;
xe.Namespace = ats.XmlRoot.Namespace;
m.XmlAttributes = new XmlAttributes ();
m.XmlAttributes.XmlElements.Add (xe);
}
mems [n] = m;
}
return mems;
}
示例13: TestSerializeXmlElementAttribute
public void TestSerializeXmlElementAttribute()
{
XmlAttributeOverrides overrides = new XmlAttributeOverrides();
XmlAttributes attr = new XmlAttributes();
XmlElementAttribute element = new XmlElementAttribute();
attr.XmlElements.Add(element);
overrides.Add(typeof(SimpleClass), "something", attr);
// null
SimpleClass simple = new SimpleClass();;
Serialize(simple, overrides);
AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"), WriterText);
// not null
simple.something = "hello";
Serialize(simple, overrides);
AssertEquals (Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something>hello</something></SimpleClass>"), WriterText);
//ElementName
element.ElementName = "saying";
Serialize(simple, overrides);
AssertEquals (Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><saying>hello</saying></SimpleClass>"), WriterText);
//IsNullable
element.IsNullable = false;
simple.something = null;
Serialize(simple, overrides);
AssertEquals(Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' />"),WriterText);
element.IsNullable = true;
simple.something = null;
Serialize(simple, overrides);
AssertEquals (Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><saying xsi:nil='true' /></SimpleClass>"), WriterText);
//Namespace
element.ElementName = null;
element.IsNullable = false;
element.Namespace = "some:urn";
simple.something = "hello";
Serialize(simple, overrides);
AssertEquals (Infoset("<SimpleClass xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><something xmlns='some:urn'>hello</something></SimpleClass>"), WriterText);
//FIXME DataType
//FIXME Form
//FIXME Type
}
示例14: BuildRequestReflectionMembers
XmlReflectionMember [] BuildRequestReflectionMembers (XmlElementAttribute optional_ns)
{
ParameterInfo [] input = MethodInfo.InParameters;
XmlReflectionMember [] in_members = new XmlReflectionMember [input.Length];
for (int i = 0; i < input.Length; i++)
{
XmlReflectionMember m = new XmlReflectionMember ();
m.IsReturnValue = false;
m.MemberName = input [i].Name;
m.MemberType = input [i].ParameterType;
m.XmlAttributes = new XmlAttributes (input[i]);
m.SoapAttributes = new SoapAttributes (input[i]);
if (m.MemberType.IsByRef)
m.MemberType = m.MemberType.GetElementType ();
if (optional_ns != null)
m.XmlAttributes.XmlElements.Add (optional_ns);
in_members [i] = m;
}
return in_members;
}
示例15: BuildResponseReflectionMembers
XmlReflectionMember [] BuildResponseReflectionMembers (XmlElementAttribute optional_ns)
{
ParameterInfo [] output = MethodInfo.OutParameters;
bool has_return_value = !(OneWay || MethodInfo.ReturnType == typeof (void));
XmlReflectionMember [] out_members = new XmlReflectionMember [(has_return_value ? 1 : 0) + output.Length];
XmlReflectionMember m;
int idx = 0;
if (has_return_value)
{
m = new XmlReflectionMember ();
m.IsReturnValue = true;
m.MemberName = Name + "Result";
m.MemberType = MethodInfo.ReturnType;
m.XmlAttributes = new XmlAttributes (MethodInfo.ReturnTypeCustomAttributeProvider);
m.SoapAttributes = new SoapAttributes (MethodInfo.ReturnTypeCustomAttributeProvider);
if (optional_ns != null)
m.XmlAttributes.XmlElements.Add (optional_ns);
idx++;
out_members [0] = m;
}
for (int i = 0; i < output.Length; i++)
{
m = new XmlReflectionMember ();
m.IsReturnValue = false;
m.MemberName = output [i].Name;
m.MemberType = output [i].ParameterType;
m.XmlAttributes = new XmlAttributes (output[i]);
m.SoapAttributes = new SoapAttributes (output[i]);
if (m.MemberType.IsByRef)
m.MemberType = m.MemberType.GetElementType ();
if (optional_ns != null)
m.XmlAttributes.XmlElements.Add (optional_ns);
out_members [i + idx] = m;
}
return out_members;
}