Reading XML file From C#
-
Hi I have xml file format mentioned "code" section. In my program, i know Failure_ID(it comes from some other binary file), based on "Failure_ID" i want to read "Name" value from XML. i want to read "Name" thru "Failure_ID". Please help me to write code for this. I am using C# windows Applcation, .net 3.5 framework Thanks
<Failure_Table>
<Failure_ID="1" Name = "Failure_1" Description = "TBD" />
<Failure_ID="2" Name = "Failure_2" Description = "TBD" />
<Failure_ID="3" Name = "Failure_3" Description = "TBD" />
</Failure_Table> -
Hi I have xml file format mentioned "code" section. In my program, i know Failure_ID(it comes from some other binary file), based on "Failure_ID" i want to read "Name" value from XML. i want to read "Name" thru "Failure_ID". Please help me to write code for this. I am using C# windows Applcation, .net 3.5 framework Thanks
<Failure_Table>
<Failure_ID="1" Name = "Failure_1" Description = "TBD" />
<Failure_ID="2" Name = "Failure_2" Description = "TBD" />
<Failure_ID="3" Name = "Failure_3" Description = "TBD" />
</Failure_Table>What have you tried ? What research have you done ? What books do you own ? The key words for your google search are XPath and XMLDocument.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
Hi I have xml file format mentioned "code" section. In my program, i know Failure_ID(it comes from some other binary file), based on "Failure_ID" i want to read "Name" value from XML. i want to read "Name" thru "Failure_ID". Please help me to write code for this. I am using C# windows Applcation, .net 3.5 framework Thanks
<Failure_Table>
<Failure_ID="1" Name = "Failure_1" Description = "TBD" />
<Failure_ID="2" Name = "Failure_2" Description = "TBD" />
<Failure_ID="3" Name = "Failure_3" Description = "TBD" />
</Failure_Table>I would have suggested that you use
SelectSingleNode
(member ofXmlDocument
), but that won't work since this is not valid XML. The problem is this part<Failure_ID="1"
, which as far as I can see quickly is never valid XML, and certainly not in this case. The problem is, there is either no element name or no attribute name (but it's impossible to tell which is missing). I suggest you change it to<Failure ID="3" Name="whatever" Description="Something">
Or if you're not in a position to change the format, use an other way to read the data - not as XML because that's not what it is. If you go for the first option, the code to select the Name based on the ID could be something likemyDocument.SelectSingleNode("/Failure_Table/Failure[@ID=\"" + theID + "\"]/Name).Value
(warning: untested, and does not do any checking so will fail if Name is not present) Hope that helps
-
Hi I have xml file format mentioned "code" section. In my program, i know Failure_ID(it comes from some other binary file), based on "Failure_ID" i want to read "Name" value from XML. i want to read "Name" thru "Failure_ID". Please help me to write code for this. I am using C# windows Applcation, .net 3.5 framework Thanks
<Failure_Table>
<Failure_ID="1" Name = "Failure_1" Description = "TBD" />
<Failure_ID="2" Name = "Failure_2" Description = "TBD" />
<Failure_ID="3" Name = "Failure_3" Description = "TBD" />
</Failure_Table>Hi the child xml nodes are invalid. you need to add element name to the child element nodes. Below format is be the correct one : <Failure_Table> <Failure Failure_ID="1" Name="Failure_1" Description="TBD" /> <Failure Failure_ID="2" Name="Failure_2" Description="TBD" /> <Failure Failure_ID="3" Name="Failure_3" Description="TBD" /> </Failure_Table> And here is the C# code : string _xmlData = "<Failure_Table>" + "<Failure Failure_ID=\"1\" Name = \"Failure_1\" Description = \"TBD\" />" + "<Failure Failure_ID=\"2\" Name = \"Failure_2\" Description = \"TBD\" />" + "<Failure Failure_ID=\"3\" Name = \"Failure_3\" Description = \"TBD\" />" + "</Failure_Table>"; int desiredFailure_ID = 2; string name; XmlDocument doc = new XmlDocument(); doc.LoadXml(_xmlData); XmlNodeList list = doc.SelectNodes(@"//Failure"); foreach (XmlNode pNode in list) { int failure_ID =int.Parse(pNode.Attributes["Failure_ID"].Value); if (failure_ID == desiredFailure_ID) name = pNode.Attributes["Name"].Value; }