Solved

reading xml file using c#?

Posted on 2012-03-12
10
243 Views
Last Modified: 2012-03-14
Hi,
How do I read xml file using c# so I can apply regex to remove <style> tags?
Then save it as xml file.

Basically I want to remove <style> tags but save value in between tags.

<customerid><style face="normal" font="default">D20001</style></customerid>


I want <customerid>D200001</customerid>

Do I use xmltextreader?
Can you some me an exmaple to read a file and save it as xml?

 input = Regex.Replace(input, "<style>(.|\n)*?</style>", string.Empty);
0
Comment
Question by:dkim18
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
10 Comments
 

Author Comment

by:dkim18
ID: 37711393
What I really want to do this after removing <style> tags are read the xmle file and put the value in List<string>

1) read xml file
2) remove <style> tags
3) read nodes and save them in array/list<string>
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 37711515
assume your xml looks something like this:

<customers>
<customerid><style face="normal" font="default">D20001</style></customerid>
<customerid><style face="normal" font="default">D20002</style></customerid>
.
.
</customers>

var list = new List<string>();
var xml  =XElement.Load("<xml_file_path");
var elements = xml.XPathSelectElements("customerid");
list = elements.Select(n=>n.Value).ToList();

Open in new window

0
 
LVL 42

Expert Comment

by:sedgwick
ID: 37711520
to use the code u need to add the following namespaces:
System.Linq
System.Xml.Xpath
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

Author Comment

by:dkim18
ID: 37711537
what happends to the<style> tag?

How do I read it  and apply the regex to remove the <style>?

I am able to read it as text, apply regex and save it.
Then read it as xml.

Wondering if I can do this by reading it as xml.
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 37711600
you dont need to apply regex or remove the <style> tag to get the value inside.
i made a mistake in the code, here's updated one:

var list = new List<string>();
var xml  =XElement.Load("<xml_file_path");
var elements = xml.XPathSelectElements("customerid/style");
list = elements.Select(n=>n.Value).ToList();

Open in new window

0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 37711609
the xml is valid even with the <style> tags, so all you gotta do is read it into XElement object and use xpath to extract the required values.
0
 

Author Comment

by:dkim18
ID: 37711653
You are right. Don't have the remove it.

Let me try the Linq.
0
 

Author Comment

by:dkim18
ID: 37714226
one more question.
How do I loop through each record (customer)?
I want to read each customer and put it in the list<string>

<customers>
<customer>
<customerid><style face="normal" font="default">D20001</style></customerid>
<address></address>
<phones>
<phone>34343</phone>
<phone>34434</phone>
</phones>
<emails>
<email>dsdsw</email>
<email>wewe</email>
</emails>
</customer>
<customer>
<customerid><style face="normal" font="default">D20002</style></customerid>
<address></address>
<phones>
<phone>122</phone>
<phone>124</phone>
<phone>12121</phone>
</phones>
<emails>
<email>dfdfdf</email>
<email>dfdf</email>
<email>dfdfd</email>
<email>dfdfd</email>
</emails>
</customer>.
</customers>
0
 

Author Comment

by:dkim18
ID: 37714797
I get all the customer records but when I cycle through each customer record, I am getting the same first record value. I am thinking this is incorrect:

  XmlNode recNum = element.SelectSingleNode("//customers/customerid/style");      



System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
               xmlDoc.Load(fileName);
               XmlNodeList elements = xmlDoc.SelectNodes("//customers/customer");
               int nCount =  elements.Count;
                Debug.WriteLine(nCount);
       
       
               foreach (XmlElement element in elements)  <=== I get all the records
               {
                   Debug.WriteLine(element.InnerText);
                 
                   XmlNode recNum = element.SelectSingleNode("//customers/customerid/style");               <== but I get the same first record value here
                   string strRecNum = recNum.InnerText;                        
                             
          XmlNode email = element.SelectSingleNode("//customers/email");
                   string strEmail = email.Attributes["name"].Value;  <== but I get the same first record value here

           
               }
0
 

Author Comment

by:dkim18
ID: 37715856
Got it working.

foreach (XmlNode element in elements)
                   
               {
                   Debug.WriteLine(element.InnerText);
                 
                   XmlNode recNum = element.SelectSingleNode("customerid/style");
                                 XmlNode email = element.SelectSingleNode("email");
               
               }
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question