0

I have a whole bunch of files that my new client's given me, he told me it was XML. I'm afraid he's way off on this one, and says he has a little programming knowledge and said he wants me to use Linq to XML because he's familiar with that.

Here's what each file looks like (Note: Replaced real data with fake data)

<UserSettings>
   ${
      Name: String;
      Age: Int32;
      Gender: String;
   }

   ${
      Name: String;
      Age: Int32;
   }
</UserSettings>

Now, this is just way too wacked out for me.

I'm trying to parse these file (and there are hundreds of them), but I have no idea where to start. I'm thinking it might be best to parse it myself, since I don't think this is anything like XML, or CSV, or Json (Actually, these files look more like Json than anything else that I've come across. But still, I don't think it's Json either.)

Has anyone seen this stuff before? What would you recommend to use to parse something like that?

4
  • Thanks @Robert H - I'll give that a go. I just found Json.NET looks pretty cool. Commented Apr 5, 2011 at 2:53
  • Huh? I can't see Robert H's comment, how come? Commented Apr 5, 2011 at 2:59
  • Ah, looks like it was deleted. Commented Apr 5, 2011 at 3:02
  • 1
    Yeah, sorry about that. Read your question again and saw that you already thought of JSON. Commented Apr 5, 2011 at 3:09

3 Answers 3

1

As Robert Harvey said, this looks like JSON. I would "pre-process" the files to make it JSON and take it from there. Basically, just run a bunch of search/replace to make it JSON -- like remove the $ with some variable name (not sure if that's legal in JSON) and surround the variables with "".

The easiest would be to do this with grep in linux or some program in windows that has a good search/replace function (like notepad++ or textpad)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, Mirkules! I am on Windows here so I can use HippoEdit. I've used that to replaced in over 180 files before in less than 2seconds. Pretty shnazzy :)
0

Yeah, I doubt an XML parser would parse those. Ideally you want to have that JSON'esque code inside CDATA tags, that way it's valid XML and can be parsed by Linq-to-XML or other XML readers. Is it something you can ask the client do change?

<UserSettings>
    <![CDATA[
    ${
        Name: String;
        Age: Int32;
        Gender: String;
    }

    ${
        Name: String;
        Age: Int32;
    }
    ]]>
</UserSettings>

2 Comments

Thanks for that @Andrey. Yeah, I'll go ask him. Should be alright, but in the meantime I plan on getting this thing parsed as soon as possible so I can sleep. I had no idea that "{" and "}" were part of valid XML? Or did I misunderstand?
{ and } are not a part of XML - by enclosing the whole thing in CDATA you tell the XML parser to treat it as a block of data and not try parse it as XML. So then you can take those blocks, strip $ and parse using any JSON library
0

This Works for me using Linq.

string xml = "<UserSettings>   ${      Name: String;      Age: Int32;      Gender: String;   }   ${      Name: String;      Age: Int32;   }</UserSettings>";

        try{
            XElement x = XElement.Parse(xml);
        }
        catch
        {
        }

Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.