本文整理汇总了C#中System.Xml.Serialization.XmlAttributes.Xmlns属性的典型用法代码示例。如果您正苦于以下问题:C# XmlAttributes.Xmlns属性的具体用法?C# XmlAttributes.Xmlns怎么用?C# XmlAttributes.Xmlns使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类System.Xml.Serialization.XmlAttributes
的用法示例。
在下文中一共展示了XmlAttributes.Xmlns属性的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
public class Student
{
[XmlAttributeAttribute]
public string Name;
[XmlNamespaceDeclarationsAttribute]
public XmlSerializerNamespaces myNamespaces;
}
public class Run
{
public static void Main()
{
Run test = new Run();
test.SerializeStudent("Student.xml");
test.DeserializeStudent("Student.xml");
}
public void SerializeStudent(string filename)
{
XmlAttributes atts = new XmlAttributes();
// Set to true to preserve namespaces,
// or false to ignore them.
atts.Xmlns=true;
XmlAttributeOverrides xover = new XmlAttributeOverrides();
// Add the XmlAttributes and specify the name of the element
// containing namespaces.
xover.Add(typeof(Student),"myNamespaces", atts);
// Create the XmlSerializer using the
// XmlAttributeOverrides object.
XmlSerializer xser = new XmlSerializer(typeof (Student),xover);
Student myStudent = new Student();
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("myns1", "http://www.cpandl.com");
ns.Add("myns2", "http://www.cohowinery.com");
myStudent.myNamespaces= ns;
myStudent.Name= "Student1";
FileStream fs = new FileStream(filename,FileMode.Create);
xser.Serialize(fs,myStudent);
fs.Close();
}
private void DeserializeStudent(string filename)
{
XmlAttributes atts = new XmlAttributes();
// Set to true to preserve namespaces, or false to ignore them.
atts.Xmlns=true;
XmlAttributeOverrides xover = new XmlAttributeOverrides();
// Add the XmlAttributes and specify the name of the
// element containing namespaces.
xover.Add(typeof(Student),"myNamespaces", atts);
// Create the XmlSerializer using the
// XmlAttributeOverrides object.
XmlSerializer xser =
new XmlSerializer(typeof (Student),xover);
FileStream fs = new FileStream(filename,FileMode.Open);
Student myStudent;
myStudent= (Student) xser.Deserialize(fs);
fs.Close();
// Use the ToArray method to get an array of
// XmlQualifiedName objects.
XmlQualifiedName[] qNames= myStudent.myNamespaces.ToArray();
for(int i = 0; i < qNames.Length;i++)
{
Console.WriteLine("{0}:{1}",
qNames[i].Name,qNames[i].Namespace);
}
}
}