How can we read XML file , line by line ?
-
Hi all. I have a XML file. something like this :
<FoxMark ELEMENTS="442">
<BOOKMARK1 ID="1" />
<BOOKMARK2 ID="2" />
<NODE4 ID="4" NAME="FireFox" DESC="" />
<BOOKMARK5 ID="5" />
<BOOKMARK6 ID="6" />
<BOOKMARK7 ID="7" />
<BOOKMARK8 ID="8" />
<BOOKMARK9 ID="9" />
<BOOKMARK10 ID="10" />
<BOOKMARK11 ID="11" />
<BOOKMARK12 ID="12" />
<BOOKMARK13 ID="13" />
</NODE4>
</FoxMark>I want to read this file, line by line and LINQ. Can you help me?
Freshman
-
Hi all. I have a XML file. something like this :
<FoxMark ELEMENTS="442">
<BOOKMARK1 ID="1" />
<BOOKMARK2 ID="2" />
<NODE4 ID="4" NAME="FireFox" DESC="" />
<BOOKMARK5 ID="5" />
<BOOKMARK6 ID="6" />
<BOOKMARK7 ID="7" />
<BOOKMARK8 ID="8" />
<BOOKMARK9 ID="9" />
<BOOKMARK10 ID="10" />
<BOOKMARK11 ID="11" />
<BOOKMARK12 ID="12" />
<BOOKMARK13 ID="13" />
</NODE4>
</FoxMark>I want to read this file, line by line and LINQ. Can you help me?
Freshman
If all you want is to investigate the lines, and you don't want to modify the XML or parse individual fields out of it, you can do it like this, no LINQ required.
var lines = File.ReadAllLines("pathToMyXMLFile.xml");
If you need to investigate individual fields or change data in the XML, I recommend looking at the XDocument[^] class. This LINQ to XML overview[^] can get you started with writing LINQ queries against XML documents.
-
If all you want is to investigate the lines, and you don't want to modify the XML or parse individual fields out of it, you can do it like this, no LINQ required.
var lines = File.ReadAllLines("pathToMyXMLFile.xml");
If you need to investigate individual fields or change data in the XML, I recommend looking at the XDocument[^] class. This LINQ to XML overview[^] can get you started with writing LINQ queries against XML documents.
Thank you very much Judah . But How we can convert XML's characters (like these: ) to the original characters.
-
Thank you very much Judah . But How we can convert XML's characters (like these: ) to the original characters.
Have a look at SecurityElement.Escape[^] method. Since you want to be working with the decoded XML special characters in the values of the XML, you might as well just use an XDoc. Here's how you can get elements in the XML by string, with the decoded special characters:
var doc = XDocument.Parse( YOUR XML GOES HERE );
var decodedElements = from element in doc.Elements()
select element.ToString();Alternately, and I don't know if this covers all your bases, you could look at System.Web.HttpUtility.HtmlDecode.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Upon this disciple I'll build my new religion? The apostle Paul, modernly speaking: Epistles of Paul Judah Himango