Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Reading XML file From C#

Reading XML file From C#

Scheduled Pinned Locked Moved C#
csharpxmlhelp
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    maheesh
    wrote on last edited by
    #1

    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>

    C L I 3 Replies Last reply
    0
    • M maheesh

      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>

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • M maheesh

        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>

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        I would have suggested that you use SelectSingleNode (member of XmlDocument), 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 like

        myDocument.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

        1 Reply Last reply
        0
        • M maheesh

          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 Offline
          I Offline
          infneeta
          wrote on last edited by
          #4

          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; }

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups