dkim18
asked on
reading xml file using c#?
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</sty le></custo merid>
I want <customerid>D200001</custo merid>
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);
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</sty
I want <customerid>D200001</custo
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>",
assume your xml looks something like this:
<customers>
<customerid><style face="normal" font="default">D20001</sty le></custo merid>
<customerid><style face="normal" font="default">D20002</sty le></custo merid>
.
.
</customers>
<customers>
<customerid><style face="normal" font="default">D20001</sty
<customerid><style face="normal" font="default">D20002</sty
.
.
</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();
to use the code u need to add the following namespaces:
System.Linq
System.Xml.Xpath
System.Linq
System.Xml.Xpath
ASKER
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.
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.
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:
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();
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
You are right. Don't have the remove it.
Let me try the Linq.
Let me try the Linq.
ASKER
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</sty le></custo merid>
<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</sty le></custo merid>
<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>
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</sty
<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</sty
<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>
ASKER
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(" //customer s/customer id/style") ;
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
xmlDoc.Load(fileName);
XmlNodeList elements = xmlDoc.SelectNodes("//cust omers/cust omer");
int nCount = elements.Count;
Debug.WriteLine(nCount);
foreach (XmlElement element in elements) <=== I get all the records
{
Debug.WriteLine(element.In nerText);
XmlNode recNum = element.SelectSingleNode(" //customer s/customer id/style") ; <== but I get the same first record value here
string strRecNum = recNum.InnerText;
XmlNode email = element.SelectSingleNode(" //customer s/email");
string strEmail = email.Attributes["name"].V alue; <== but I get the same first record value here
}
XmlNode recNum = element.SelectSingleNode("
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
xmlDoc.Load(fileName);
XmlNodeList elements = xmlDoc.SelectNodes("//cust
int nCount = elements.Count;
Debug.WriteLine(nCount);
foreach (XmlElement element in elements) <=== I get all the records
{
Debug.WriteLine(element.In
XmlNode recNum = element.SelectSingleNode("
string strRecNum = recNum.InnerText;
XmlNode email = element.SelectSingleNode("
string strEmail = email.Attributes["name"].V
}
ASKER
Got it working.
foreach (XmlNode element in elements)
{
Debug.WriteLine(element.In nerText);
XmlNode recNum = element.SelectSingleNode(" customerid /style");
XmlNode email = element.SelectSingleNode(" email");
}
foreach (XmlNode element in elements)
{
Debug.WriteLine(element.In
XmlNode recNum = element.SelectSingleNode("
XmlNode email = element.SelectSingleNode("
}
ASKER
1) read xml file
2) remove <style> tags
3) read nodes and save them in array/list<string>