Making Variable read from XML C#

spirosart

New Member
Based on some of my previous questions I have put together the following:\[code\]var xml = "<root><Data><Info>data</Info></Data></root>"; var xmlString = (from data in XElement.Parse(xml).Descendants("Data") where data.Descendants().Any() select data.Descendants().First().Value).FirstOrDefault(); var fileString = File.ReadAllText(@"1.txt"); Console.WriteLine("{0}", fileString == xmlString ? 1 : 0);\[/code\]The above is supposed to load a specific part of an xml, load a text file, and compare the two. Unfortunatly for me, I am new to C# and some of its concepts, and am unsure of how to make the \[code\]var xml = "<root><Data><Seperator>data</Seperator></Data></root>";\[/code\] read from a specific xml file in the same directory.I know I can use:\[code\] FileStream stream = new FileStream("1234.xml", FileMode.Open);XmlReader reader = new XmlTextReader(stream);while(reader.Read()){ Console.WriteLine(reader.Value);}\[/code\]if I were to have 1234.xml and wanted to output it to the console, but I don't know how to load the contents of the file in a way that the defined variable xml is able to read it.EDIT:Based on the posts below, I have tried to use the following, but it is returning blank in console:\[code\] using(XmlReader reader = XmlReader.Create("2.xml")) { while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { if (reader.Name.ToLower() == "data") { string xml = reader.ReadOuterXml(); var xmlString = (from data in XDocument.Parse(xml).Elements() select data.Elements().First().Value).FirstOrDefault(); xmlString = xmlString.Replace("\n", "").Trim(); var fileString = File.ReadAllText(@"8.txt"); Console.WriteLine("{0}", fileString == xmlString ? 1 : 0); } } } }\[/code\]Edit 2:The XML file is a hash value and a signature (I am only trying to grab and compare the Hash and its value):\[code\]<?xml version="1.0"?>-<root>-<Data><Info>Hash: xxxxxxxxxxxxxxxxxxxxxxxxxxx</Info></Data>-<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">-<SignedInfo><CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/><SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>-<Reference URI="">-<Transforms><Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/></Transforms><DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/><DigestValue>GHOSJRUqcHnZ3M090/5/KhvghyQ=</DigestValue></Reference></SignedInfo><SignatureValue>oib+LAXqJshDFm3YM63qkSsJxxF+t0uahGax8tLrjSPJUjW045iYvB4LJCgMeF9oxatbWnVB9hGbvtVnl4iewJDL3kjnjvot5CLozMOaIGJgdys5MP8ncx771itANTm8wi8KgnqVjGjvTakEmcdwcSdRXuCP1WGOwuXm5StkY8Q=</SignatureValue></Signature></root>\[/code\]
 
Back
Top