rippergr
rippergr

Reputation: 184

Split text into variables based on pattern c#

I have this form of text string

<remittance><contain><blrn>032398106/17/59321</blrn><brmt>17GR0900FG344</brmt><bamnt>0</bamnt><bmsv>120,00</bmsv></contain></remittance>

Is there a way to get every node into variables so I can use them individual as variables in my form?

I want something like this

blrn = "032398106/17/59321"
brmt = "17GR0900FG344"
bamnt = "0"
bmsv = "120,00"

Itried a code like this

    string data = "&lt;remittance&gt;&lt;contain&gt;&lt;blrn&gt;032398106/17/59321&lt;/blrn&gt;&lt;brmt&gt;lrn /afm check error&lt;/brmt&gt;&lt;bamnt&gt;0&lt;/bamnt&gt;&lt;bmsv&gt;0&lt;/bmsv&gt;&lt;/contain&gt;&lt;/remittance&gt;";
    var decoded = System.Web.HttpUtility.HtmlDecode(data);

    string[] export;
    export = decoded.Replace("<blrn>", "|").Split('|');

but it doesnt return the corect values to export

Upvotes: 2

Views: 58

Answers (1)

Ctznkane525
Ctznkane525

Reputation: 7465

This text is essentially an Xml Document. You can get the content this way:

System.Xml.XmlDocument xd = new System.Xml.XmlDocument();
xd.LoadXml(yourxmlstring);
System.Xml.XmlElement root = xd.DocumentElement;
System.Xml.XmlNodeList nl = root.SelectNodes("//remittance/contain");
Dictionary<string, string> dictionary = new Dictionary<string, string>();
foreach (System.Xml.XmlNode xnode in nl.Item(0).ChildNodes)
{
    string name = xnode.Name;
    string value = xnode.InnerText;
    dictionary.Add(name, value);
    }

Upvotes: 3

Related Questions