将XML数据读入C#应用程序
我需要创建一个xml文件,它应该是这样的将XML数据读入C#应用程序,c#,xml,xml-serialization,C#,Xml,Xml Serialization,我需要创建一个xml文件,它应该是这样的 <Event id="Date - Event Name - Event Type"> <DogNumber id="dog id number"> <dogName id-"dog id number">dog name</dogName> <dogBreed>dog breed</d
<Event id="Date - Event Name - Event Type">
<DogNumber id="dog id number">
<dogName id-"dog id number">dog name</dogName>
<dogBreed>dog breed</dogBreed>
</DogNumber>
</Event>
狗名
犬种
然后,除了元素的不同值和属性之外,它会对另一个事件再次重复
<Event id="Date - Event Name - Event Type">
<DogNumber id="dog id number">
<dogName id-"dog id number">dog name</dogName>
<dogBreed>dog breed</dogBreed>
</DogNumber>
</Event>
狗名
犬种
我不熟悉用C#创建XML文件,并且在正确地将属性添加到元素以及获取与上面所示相同的父节点和子节点时遇到了问题。我需要能够从我的C#应用程序中查看此文件,并能够根据特定事件以及每个事件中的特定狗读取上面列出的所有值。选择哪个事件和哪个狗的标准将基于用户在组合框中的输入。我的计划是使用
getElementById
方法。然而,我已经看到了很多不同的方法来实现这一点,以至于我很难决定什么是最好、最有效的方法。我发现,对于XML,最好的方法是使用类进行序列化/反序列化
例如,以以下代码为例:
/// <summary>
/// Details on the destination of the shipment.
/// </summary>
[XmlRoot("destination")]
public class Destination
{
List<Recipient> recipient { get; set; }
}
/// <summary>
/// Recipient details.
/// </summary>
[XmlRoot("recipient")]
public class Recipient
{
/// <summary>
/// Client Id of the recipient; only used if selected as the sort criterion.
/// </summary>
/// <remarks>Truncated after 30 characters.</remarks>
[XmlElement("client-id")]
public string ClientID { get; set; }
/// <summary>
/// Name of the individual receiving the shipment.
/// </summary>
/// <remarks>Truncated after 44 characters.</remarks>
[XmlElement("contact-name")]
public string ContactName { get; set; }
/// <summary>
/// Name of the company.
/// </summary>
/// <remarks>Truncated after 44 characters.</remarks>
[XmlElement("company")]
public string Company { get; set; }
...
//
///装运目的地的详细信息。
///
[XmlRoot(“目的地”)]
公务舱目的地
{
列表收件人{get;set;}
}
///
///收件人详细信息。
///
[XmlRoot(“收件人”)]
公共类收件人
{
///
///收件人的客户端Id;仅在选择作为排序标准时使用。
///
///在30个字符后截断。
[XmlElement(“客户id”)]
公共字符串ClientID{get;set;}
///
///收货人的姓名。
///
///在44个字符后截断。
[XmlElement(“联系人姓名”)]
公共字符串ContactName{get;set;}
///
///公司名称。
///
///在44个字符后截断。
[XmlElement(“公司”)]
公共字符串公司{get;set;}
...
这将允许我将对象序列化为如下所示的XML:
<destination>
<recipient>
<client-id></client-id>
<contact-name></contact-name>
<company></company>
...
...
我可以通过使用XmlRoot或XmlElement修饰符来控制XML的创建方式。在您的情况下,可以使用[XmlAttribute(“attributeName”)]
来指定属性
您可以在这里阅读有关各种XML修饰符的更多信息:。我发现,对于XML,最好的方法是使用类进行序列化/反序列化 例如,以以下代码为例:
/// <summary>
/// Details on the destination of the shipment.
/// </summary>
[XmlRoot("destination")]
public class Destination
{
List<Recipient> recipient { get; set; }
}
/// <summary>
/// Recipient details.
/// </summary>
[XmlRoot("recipient")]
public class Recipient
{
/// <summary>
/// Client Id of the recipient; only used if selected as the sort criterion.
/// </summary>
/// <remarks>Truncated after 30 characters.</remarks>
[XmlElement("client-id")]
public string ClientID { get; set; }
/// <summary>
/// Name of the individual receiving the shipment.
/// </summary>
/// <remarks>Truncated after 44 characters.</remarks>
[XmlElement("contact-name")]
public string ContactName { get; set; }
/// <summary>
/// Name of the company.
/// </summary>
/// <remarks>Truncated after 44 characters.</remarks>
[XmlElement("company")]
public string Company { get; set; }
...
//
///装运目的地的详细信息。
///
[XmlRoot(“目的地”)]
公务舱目的地
{
列表收件人{get;set;}
}
///
///收件人详细信息。
///
[XmlRoot(“收件人”)]
公共类收件人
{
///
///收件人的客户端Id;仅在选择作为排序标准时使用。
///
///在30个字符后截断。
[XmlElement(“客户id”)]
公共字符串ClientID{get;set;}
///
///收货人的姓名。
///
///在44个字符后截断。
[XmlElement(“联系人姓名”)]
公共字符串ContactName{get;set;}
///
///公司名称。
///
///在44个字符后截断。
[XmlElement(“公司”)]
公共字符串公司{get;set;}
...
这将允许我将对象序列化为如下所示的XML:
<destination>
<recipient>
<client-id></client-id>
<contact-name></contact-name>
<company></company>
...
...
我可以通过使用XmlRoot或XmlElement修饰符来控制XML的创建方式。在您的情况下,可以使用[XmlAttribute(“attributeName”)]
来指定属性
您可以在此处阅读有关各种XML修饰符的更多信息:.在C中#对于创建或读取XML,您需要使用(您在本msdn网站末尾有使用示例)
在处理(添加节点等)之后,如果需要,可以将xml保存在file.xml中
// verify if file not existing
if (!System.IO.File.Exists(@"yourName.xml"))
{
// if file not exist, create xml file.
FileStream fs = File.Create(@"yourName.xml");
fs.Close();
}
// save your xml in xml file
yourXml.Save(@"yourName.xml");
在C#中,对于创建或读取XML,您需要使用(在本msdn网站的末尾有使用示例)
在处理(添加节点等)之后,如果需要,可以将xml保存在file.xml中
// verify if file not existing
if (!System.IO.File.Exists(@"yourName.xml"))
{
// if file not exist, create xml file.
FileStream fs = File.Create(@"yourName.xml");
fs.Close();
}
// save your xml in xml file
yourXml.Save(@"yourName.xml");
您可以尝试使用XDocument,如:
XDocument doc = new XDocument(new XElement("Event", new XAttribute("Id", youEventString),
new XElement("DogNumber", new XAttribute("id", dogId),
new XElement("dogName", new XAttribute("id", dogNumber), dogNname),
new XElement("dogBreed", dogBreed)
)));
doc.Save(filename);
您可以用适当的字符串替换youEventString、dogId、dogNumber、dogName、DogBride。还可以提供要保存文件的文件名
祝您好运您可以尝试使用XDocument,如:
XDocument doc = new XDocument(new XElement("Event", new XAttribute("Id", youEventString),
new XElement("DogNumber", new XAttribute("id", dogId),
new XElement("dogName", new XAttribute("id", dogNumber), dogNname),
new XElement("dogBreed", dogBreed)
)));
doc.Save(filename);
您可以用适当的字符串替换youEventString、dogId、dogNumber、dogName、DogBride。还可以提供要保存文件的文件名
祝你好运检查LINQ到XML
检查LINQ到XML
我认为读/写xml文件最简单的方法是使用XMLSerializer。这种方法还使您能够轻松地绑定到数据(如果您使用WPF for UI) 创建可序列化的类:
public class Event
{
[XmlAttribute]
public string id { get; set; }
[XmlElement]
public DogNumber DogNumber { get; set; }
}
public class DogNumber
{
[XmlAttribute]
public string id { get; set; }
[XmlElement]
public dogName dogName { get; set; }
[XmlElement]
public string dogBreed { get; set; }
}
public class dogName
{
[XmlAttribute]
public string id { get; set; }
[XmlTextAttribute]
public string value { get; set; }
}
然后使用XmlSerializer进行反序列化(例如使用文件):
我认为读/写xml文件最简单的方法是使用XMLSerializer。这种方法还使您能够轻松地绑定到数据(如果您使用WPF for UI) 创建可序列化的类:
public class Event
{
[XmlAttribute]
public string id { get; set; }
[XmlElement]
public DogNumber DogNumber { get; set; }
}
public class DogNumber
{
[XmlAttribute]
public string id { get; set; }
[XmlElement]
public dogName dogName { get; set; }
[XmlElement]
public string dogBreed { get; set; }
}
public class dogName
{
[XmlAttribute]
public string id { get; set; }
[XmlTextAttribute]
public string value { get; set; }
}
然后使用XmlSerializer进行反序列化(例如使用文件):
不,我没有。我正在努力学习如何动态地阅读和编写xml。我希望使用类似的方法将数据加载回程序.xml.SelectNodes(“/Names/Name[@id='dogId']”中);没有。我正在努力学习如何快速阅读和编写xml。我希望使用类似的方法将数据加载回program.xml.Sele