我在存储xml样式文本的程序中有一个字符串,我想分配
我只能使用C#,只能使用.NET 2.0和.NET 3.5
<Document>
<IdSprawy>vff24</IdSprawy>
<TaskNumber>0173196</TaskNumber>
<TestText>ferf24</TestText>
</Document>
我想要最终结果:(理论上,这样的结果是)
string Id = 'vff24';
string TaskNumber = '0173196';
string TestText = 'ferf24';
我不知道该怎么做
我开始这样写某事:
public class A_StartActSerScr
{
public static void OnFormExit()
{
string TextXML; // this xml
// here i want to assign data from xml to variables via the "Document" class
// for exp.
string IdSprawyX = 'vff24';
string TaskNumberX = '0173196';
string TestTextX = 'ferf24';
}
}
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public class Document
{
[System.Xml.Serialization.XmlElement("IdSprawy")]
public string IdSprawyField{ get; set; }
[System.Xml.Serialization.XmlElement("TaskNumber")]
public string TaskNumberField{ get; set; }
[System.Xml.Serialization.XmlElement("TestText")]
public string TestTextField { get; set; }
}
我不知道如何正确编写类:
以及如何编写反序列化函数:
我觉得这个问题,使用XmlSerializer的回答你的问题,反序列化。
另一个选择是使用Linq to Xml来解析您的XML字符串。下面的代码不一定经过优化,但是它将xml解析为单独的值。
string xml = "<Document><Id>vff24</Id><TaskNumber>0173196</TaskNumber><TestText>ferf24</TestText></Document>";
var xEl = System.Xml.Linq.XElement.Parse(xml);
string id = xEl.Element("Id").Value;
string taskNumber = xEl.Element("TaskNumber").Value;
string testText = xEl.Element("TestText").Value;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句