-4

This is the current format i have from external source

<d:entry id="_qkq" d:title="DuMont Television Network">
        <d:index d:value="DuMont Television Network" d:title="DuMont Television Network"/>
        <d:index d:value="dumont tv" d:title="DuMont Television Network"/>
        <d:index d:value="dumont network" d:title="DuMont Television Network"/>
        <h1>DuMont Television Network</h1>
       some value etc 
</d:entry>

If it was something like below one i could have parsed it easily. So any idea how can i parse it like below or convert it to below format?

<entry>
   <entryId>_qkq</entryId>
   <entryTitle>DuMont Television Network</entryTitle>
   <entryIndex>
      <value>DuMont Television Network</value>
      <title>DuMont Television Network</title>
   </entryIndex>
   <entryIndex>
      <value>dumont tv</value>
      <title>DuMont Television Network</title>
   </entryIndex>
   <entryIndex>
      <value>dumont network</value>
      <title>DuMont Television Network</title>
   </entryIndex>
   <entryValue>
      <h1>DuMont Television Network</h1>
      some value etc
   </entryValue>
</entry>

Thank you very much for answers

Furkan Gözükara
  • 22,964
  • 77
  • 205
  • 342
  • You could use this: https://msdn.microsoft.com/de-de/library/system.xml.linq.xelement(v=vs.110).aspx – Mighty Badaboom Mar 17 '17 at 12:28
  • 3
    I'll repeat a comment from the previous time you tried asking this question. This is not a code writing service, try something and come back with a specific problem providing a [MCVE]. – TheLethalCoder Mar 17 '17 at 12:29
  • To make it easier for you, are you having problem parsing the `d:` part of the element names or getting access to the attributes? – TheLethalCoder Mar 17 '17 at 12:30
  • @TheLethalCoder if i had idea i wouldnt ask here. if you have any ideas for this problem feel free to provide. and no i dont have any definition anyhwere about d: – Furkan Gözükara Mar 17 '17 at 12:30
  • I doubt that you have *absolutely no idea*. Do you know how to parse the original XML at all? If so, that's one part you have an idea for. Now, what's the next step? – Jon Skeet Mar 17 '17 at 12:32
  • @JonSkeet yes i know. as i shown in the question, if the format was like the below one i could parse. but currently it is not. so am i asking ideas to how to convert or parse like that below one. – Furkan Gözükara Mar 17 '17 at 12:34
  • Mate, can you post the whole xml (with ` xml` opening tags, namespace declarations etc). – zaitsman Mar 17 '17 at 12:36
  • As you knew this already that it is not a standard xml to parse into your specific format than you should have made a parser for this by yourself – J.SMTBCJ15 Mar 17 '17 at 12:37
  • @zaitsman it starts as this and no other information : – Furkan Gözükara Mar 17 '17 at 12:38
  • @J.SMTBCJ15 ok show me the way of making such parser if you have idea. – Furkan Gözükara Mar 17 '17 at 12:39
  • 1
    Note that xmlns:d. It states to which namespace alias "d" corresponds. Then read how to parse xml with namespaces (there are a ton questions here and all over internet about how to do that). – Evk Mar 17 '17 at 12:41
  • @TheLethalCoder already told you about this that SO is not coding service. But helping people is a good thing if they have time for this. – J.SMTBCJ15 Mar 17 '17 at 12:42
  • @J.SMTBCJ15 On your second point helping writing code for people when they have put no effort in is then a job or a code writing service. There are places out there to get people to do that for you. However, SO is not one of those places, although sadly it does happen. – TheLethalCoder Mar 17 '17 at 12:47
  • Agreed with you @TheLethalCoder – J.SMTBCJ15 Mar 17 '17 at 12:50
  • "if the format was like the below one i could parse" - but the first version is still valid XML, so you should still be able to parse it just as easily, e.g. using `XDocument.Parse` or `XDocument.Load`. What you do with it is the next step. – Jon Skeet Mar 17 '17 at 13:39

1 Answers1

2

Okay, this is fairly easy and can be done in many many ways, but for ease of development:

  1. Generate the classes for this XML - in Visual Studio, create new code file. Copy the full XML into clipboard. Click Edit-paste special - Paste XML as classes.
    This will give you the necessary classes, with the root being dictionary. (If you want i can add the listing generated)
  2. Then, in whatever file necessary you can do the following:

      var data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \r\n<d:dictionary xmlns=\"w3.org/1999/xhtml\" xmlns:d=\"apple.com/DTDs/DictionaryService-1.0.rng\">\r\n\t<d:entry id=\"_qkq\" d:title=\"DuMont Television Network\">\r\n\t\t<d:index d:value=\"DuMont Television Network\" d:title=\"DuMont Television Network\"/>\r\n\t\t<d:index d:value=\"dumont tv\" d:title=\"DuMont Television Network\"/>\r\n\t\t<d:index d:value=\"dumont network\" d:title=\"DuMont Television Network\"/>\r\n\t\t<h1>DuMont Television Network</h1>\r\n\t</d:entry>\r\n</d:dictionary>";
    
      XmlSerializer serializer = new XmlSerializer(typeof(dictionary));
      using (TextReader reader = new StringReader(data))
      {
        dictionary result = (dictionary)serializer.Deserialize(reader);
      }
    

The result now contains your data:

screenshot

zx485
  • 28,498
  • 28
  • 50
  • 59
zaitsman
  • 8,984
  • 6
  • 47
  • 79