Asked by:
Read xml

Question
-
User-588203678 posted
Hi
I have nlog.config
<targets>
<Target name ="file" fileName=""x:/error.log">
>
<Target name="Json" fileName="c:/json.json>
I want to read target name.read the fileName.
Similarly json target fileName.
Friday, October 25, 2019 2:58 AM
All replies
-
User-1038772411 posted
Hello guhananth1,
As you show nglog.config file but it is not show proper xml. There is some missing end tag.
So attached correct nglog.config file
<targets> <Target name ="file" fileName="x:/error.log"/> <Target name="Json" fileName="c:/json.json"/> </targets>
The code below is .cs file code.
XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(@"D:\XML\nglog.config"); XmlNodeList xnList = xmlDocument.SelectNodes("/targets/Target"); Dictionary<string, string> listOfTarget = new Dictionary<string, string>();
int x=0; foreach (XmlNode xn in xnList) { listOfTarget.Add("fileName "+x, xn.Attributes["fileName"].Value);
x=x+1; }The dictionary listOfTarget shows list of filename of all the elements which are in <targets>----</targets>
If you still not getting result as you want let us know.
Thanks.
Friday, October 25, 2019 4:14 AM -
User-588203678 posted
It says empty path name is not legalFriday, October 25, 2019 5:11 AM -
User-1038772411 posted
Hello guhananth1,
We have checked again. You can use below config File. Because your config file has not end tag.
And also missing root tag. So that We have attached new nlog.config file.
<targets> <Target name ="file" fileName="x:/error.log"/> <Target name="Json" fileName="c:/json.json"/> </targets>
Also you have to set path your file path into the line
xmlDocument.Load(@"YOUR FILE PATH HERE");
If you still not getting result then please share your code and file that you have used.
Thanks.
Friday, October 25, 2019 5:49 AM -
User-588203678 posted
Nlog.config has
<Nlog>
<target>
<targets name="file" fileName="c:/nlog.txt">
<targets name="json" fileName="c:/Jon.json">
<\target>Friday, October 25, 2019 6:08 AM -
User-1038772411 posted
hello guhananth1,
As you share your Nlog.config but the file you have share is not correct file.
If your file start with <Nlog> then your file must have tag </Nlog>
And also in your file no end of targets tag. so We attached below file with <Nlog> tag.
<Nlog> <targets> <Target name ="file" fileName="x:/error.log"/> <Target name="Json" fileName="c:/json.json"/> </targets> </Nlog>
And in your .cs file you have to just below changes
XmlNodeList xnList1 = xmlDocument1.SelectNodes("/Nlog/targets/Target");
If you have still any issue then please attached your .cs file code and correct .config file so that Identify where is the actual problem.
Thanks.
Friday, October 25, 2019 6:45 AM -
User-588203678 posted
Count is zeroFriday, October 25, 2019 6:54 AM -
User-1038772411 posted
Hello guhananth1,
Think that you are using below .config file then you will not get count Zero.
<Nlog> <targets> <Target name ="file" fileName="x:/error.log"/> <Target name="Json" fileName="c:/json.json"/> </targets> </Nlog>
Still you are facing issue then please share your .config file and .cs file so that check the actual problem.
Thanks.
Friday, October 25, 2019 7:05 AM -
User-588203678 posted
Hi
In rule tag,how to read logger name attribute?Monday, October 28, 2019 8:25 AM -
User665608656 posted
Hi guhananth1,
In rule tag,how to read logger name attribute?To read the logger name attribute, you can refer to this code:
XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(@"your nlog path"); XmlNodeList logger = xmlDocument.GetElementsByTagName("logger"); for (int i = 0; i < logger.Count; i++) { string attrVal = logger[i].Attributes["name"].Value; Console.Write(attrVal); }
Best Regards,
YongQing.
Tuesday, October 29, 2019 7:23 AM