本文整理汇总了C#中System.Xml.Serialization.XmlAttributeAttribute.AttributeName属性的典型用法代码示例。如果您正苦于以下问题:C# XmlAttributeAttribute.AttributeName属性的具体用法?C# XmlAttributeAttribute.AttributeName怎么用?C# XmlAttributeAttribute.AttributeName使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类System.Xml.Serialization.XmlAttributeAttribute
的用法示例。
在下文中一共展示了XmlAttributeAttribute.AttributeName属性的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
public class Group
{
// Change the XML attribute name.
[XmlAttribute(AttributeName = "MgrName")]
public string Name;
/* Use the AttributeName to collect all the XML attributes
in the XML-document instance. */
}
public class Run
{
public static void Main()
{
Run test = new Run();
/* To use the AttributeName to collect all the
XML attributes. Call SerializeObject to generate
an XML document and alter the document by adding
new XML attributes to it. Then comment out the SerializeObject
method, and call DeserializeObject. */
test.SerializeObject("MyAtts.xml");
test.DeserializeObject("MyAtts.xml");
}
public void SerializeObject(string filename)
{
Console.WriteLine("Serializing");
// Create an instance of the XmlSerializer class.
XmlSerializer mySerializer = new XmlSerializer(typeof(Group));
// Writing the file requires a TextWriter.
TextWriter writer = new StreamWriter(filename);
// Create an instance of the class that will be serialized.
Group myGroup = new Group();
/* Set the Name property, which will be generated
as an XML attribute. */
myGroup.Name = "Wallace";
// Serialize the class, and close the TextWriter.
mySerializer.Serialize(writer, myGroup);
writer.Close();
}
public void DeserializeObject(string filename)
{
Console.WriteLine("Deserializing");
XmlSerializer mySerializer =
new XmlSerializer(typeof(Group));
FileStream fs = new FileStream(filename, FileMode.Open);
Group myGroup = (Group)
mySerializer.Deserialize(fs);
Console.WriteLine(myGroup.Name);
}
}
示例2: Main
//引入命名空间
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
public class MainClass {
public static void Main() {
Product pd = new Product();
pd.ProductID = 200;
pd.CategoryID = 100;
pd.Discontinued = false;
pd.ProductName = "Serialize Objects";
pd.QuantityPerUnit = "6";
pd.ReorderLevel = 1;
pd.SupplierID = 1;
pd.UnitPrice = 1000;
pd.UnitsInStock = 10;
pd.UnitsOnOrder = 0;
TextWriter tr = new StreamWriter("serialprod.xml");
XmlSerializer sr = new XmlSerializer(typeof(Product));
sr.Serialize(tr, pd);
tr.Close();
}
}
[System.Xml.Serialization.XmlRootAttribute()]
public class Product {
private int prodId;
private string prodName;
private int suppId;
private int catId;
private string qtyPerUnit;
private Decimal unitPrice;
private short unitsInStock;
private short unitsOnOrder;
private short reorderLvl;
private bool discont;
private int disc;
//added the Discount attribute
[XmlAttributeAttribute(AttributeName = "Discount")]
public int Discount {
get { return disc; }
set { disc = value; }
}
[XmlElementAttribute()]
public int ProductID {
get { return prodId; }
set { prodId = value; }
}
[XmlElementAttribute()]
public string ProductName {
get { return prodName; }
set { prodName = value; }
}
[XmlElementAttribute()]
public int SupplierID {
get { return suppId; }
set { suppId = value; }
}
[XmlElementAttribute()]
public int CategoryID {
get { return catId; }
set { catId = value; }
}
[XmlElementAttribute()]
public string QuantityPerUnit {
get { return qtyPerUnit; }
set { qtyPerUnit = value; }
}
[XmlElementAttribute()]
public Decimal UnitPrice {
get { return unitPrice; }
set { unitPrice = value; }
}
[XmlElementAttribute()]
public short UnitsInStock {
get { return unitsInStock; }
set { unitsInStock = value; }
}
[XmlElementAttribute()]
public short UnitsOnOrder {
get { return unitsOnOrder; }
set { unitsOnOrder = value; }
}
[XmlElementAttribute()]
public short ReorderLevel {
get { return reorderLvl; }
set { reorderLvl = value; }
}
[XmlElementAttribute()]
public bool Discontinued {
get { return discont; }
set { discont = value; }
}
}
public class Inventory {
private Product[] stuff;
public Inventory() { }
[XmlArrayItem("Prod", typeof(Product)),
XmlArrayItem("Book", typeof(BookProduct))]
public Product[] InventoryItems {
get { return stuff; }
set { stuff = value; }
}
}
public class BookProduct : Product {
private string isbnNum;
public BookProduct() { }
public string ISBN {
get { return isbnNum; }
set { isbnNum = value; }
}
}