怎么用C#生成一个包含以下内容的xml文件呢?

2024-12-21 18:00:26
推荐回答(4个)
回答1:

使用 序列化XML文档 方式

using System;
using System.IO;
using System.Xml.Serialization;

namespace MySerializableXML
{
[Serializable]
public class SerializableXML
{
//xmlfile.xml 存放的路径 ,此处放于debug下
private static string xpath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory" ,"xmlfile.xml");
// 序列化
public static void Serialize(Feature f)
{
XmlSerializer xs = new XmlSerializer(typeof(Feature));
Stream stream = new FileStream(xpath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
xs.Serialize(stream, f);
stream.Close();
}
// 反序列化
public static void DeSerialize()
{
XmlSerializer xs = new XmlSerializer(typeof(Feature));
Stream stream = new FileStream(xpath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
Feature ft = (Feature)xs.Deserialize(stream);
stream.Close();
}
}
[Serializable]
public class Feature
{
private string _FormID;
[XmlAttribute("FormID")]
public string FormID { get { return _FormID; } set { _FormID = value; } }
private string _FeatureID;
[XmlAttribute("FeatureID")]
public string FeatureID { get { return _FeatureID; } set { _FeatureID = value; } }
private string _Type;
[XmlAttribute("Type")]
public string Type { get { return _Type; } set { _Type = value; } }
private Name _name;
[XmlElement("Name")]
public Name Name{ get { return _name; }set { _name = value; }}
private Values _Values;
[XmlElement("Values")]
public Values Values{get { return _Values; }set { _Values = value; }}
}
[Serializable]
public class Name
{
private string _text;
[XmlText]
public string Text{get { return _text; } set { _text = value; } }
}
[Serializable]
public class Values
{
private Value _valuere;
[XmlElement("Value")]
public Value Valuere{ get { return _valuere; }set { _valuere = value; } }
}
[Serializable]
public class Value
{
private string _text;
[XmlText]
public string Text {get { return _text; } set { _text = value; }}
}
}

//******************************************调用方法

class Program
{
static void Main(string[] args)
{
Feature f = new Feature();
Name n = new Name();
n.Text = "Simulation Type";
Values valus = new Values();
Value v = new Value();
v.Text = "1";
valus.Valuere = v;
f.Name = n;
f.Values = valus;
f.FormID = "55c48978-522e-4fee-ad44-65dd13042d91";
f.FeatureID = "82e15513-7a26-42e3-b2f8-96828c806613";
SerializableXML.Serialize(f);
}
}

//***********************直接复制代码就可运行,注意命名空间 和引用
//***********************我是菜鸟*********************************************

回答2:

Console.Write("I am \"Peter\".");

试一下这样,使用转义符\,然后后面加引号

回答3:

string str=@"""55c48978-522e-4fee-ad44-65dd13042d91""";

回答4:

在xml文件中双引号,表示单引号,记住这点就行了