Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将字符串XML分离为字符串变量|序列化?_C#_.net_Xml - Fatal编程技术网

C# 将字符串XML分离为字符串变量|序列化?

C# 将字符串XML分离为字符串变量|序列化?,c#,.net,xml,C#,.net,Xml,我在程序中有一个字符串,用于存储xml样式的文本,我想指定 我只能使用C#和.NET2.0i.NET3.5 <Document> <IdSprawy>vff24</IdSprawy> <TaskNumber>0173196</TaskNumber> <TestText>ferf24</TestText> </Document> 我完全不知道怎么做 我开始写这样的东

我在程序中有一个字符串,用于存储xml样式的文本,我想指定

我只能使用C#和.NET2.0i.NET3.5

  <Document>
    <IdSprawy>vff24</IdSprawy>
    <TaskNumber>0173196</TaskNumber>
    <TestText>ferf24</TestText>
   </Document>

我完全不知道怎么做

我开始写这样的东西:

    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进行反序列化的问题

另一个选项是用来解析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;
string xml=“vff240173196ferf24”;
var xEl=System.Xml.Linq.XElement.Parse(Xml);
字符串id=xEl.Element(“id”).Value;
字符串taskNumber=xEl.Element(“taskNumber”).Value;
字符串testText=xEl.Element(“testText”).Value;
我认为这回答了您使用XmlSerializer进行反序列化的问题

另一个选项是用来解析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;
string xml=“vff240173196ferf24”;
var xEl=System.Xml.Linq.XElement.Parse(Xml);
字符串id=xEl.Element(“id”).Value;
字符串taskNumber=xEl.Element(“taskNumber”).Value;
字符串testText=xEl.Element(“testText”).Value;

“IdSprawy”
应该是xml中的
“Id”
。如果有
XmlSerializer,为什么要编写反序列化函数。反序列化(myObjec)
“IdSprawy”
应该是xml中的
“Id”
。如果有
XmlSerializer.DeSerialize(myObjec),为什么要编写反序列化函数