1

I have a string in the program that stores xml-style text, I would like to assign

I can use only C# and only .NET 2.0 i .NET 3.5

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

I want final result: (theoretically, such results would be)

string Id = 'vff24';
string TaskNumber = '0173196';
string TestText = 'ferf24';

I don't know completely how to do it

I start writting sth like this:

    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; }
    }

I dont know hot to write classes correctly:

and how to write deserialize function:

1 Answers1

0

I think this question answers your deserialization question using XmlSerializer.

Another option would be to use Linq to Xml to parse your XML string. The below code isn't necessarily optimized, but it parses the xml into separate values.

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;
A-A-ron
  • 529
  • 1
  • 5
  • 14