2

Is there a 'standard' way to convert something like this into a set of C# classes?

<!DOCTYPE MESSAGES [
<!ELEMENT MESSAGES (MSG*)>
<!ELEMENT MSG (TO+,BODY,MSG_ID,BILLING)>
<!ATTLIST MSG TYPE (CONTENT|LOGO|RINGTONE|PICTURE|OTAPUSH|BINFWD|LONGSMS|2DCODE) #REQUIRED>
<!ELEMENT TO (#PCDATA)>
<!ATTLIST TO PROVID (1|2|3|5|6|7) #IMPLIED >
<!ATTLIST TO TYPE (NPM|EMS) #IMPLIED>
<!ELEMENT BODY (#PCDATA)>
<!ELEMENT MSG_ID (#PCDATA)>
<!ELEMENT BILLING (#PCDATA)>
<!ELEMENT DELIVERY (#PCDATA)>
<!ELEMENT EXPDATE (#PCDATA)>
]>

I have some more information but would greatly appreciate a way to transform the ELEMENT and ATTLIST into C#.

Rudi
  • 3,124
  • 26
  • 35

3 Answers3

4

Do you mean generate an instance of this class from this data, or generate a class definition from this data? For the latter, you can use an XSD to C# generator to get a class definition that would hold this information. For the former...it's more complicated. See below.

Is this data inside a CDATA element?

If so, converting this into an instance of a C# class might be a little more difficult. If not, it's actually fairly simple.

Take a look at LINQ-to-XML: http://msdn.microsoft.com/en-us/library/bb387061.aspx

I've used LINQ to XML to parse XML files directly into classes this way:

List<Parameter> tempList = (from param in x.Descendants("Parameter")
                              select new Parameter
                              {
                                  Name = param.Attribute("Name").Value,
                                  Value = param.Attribute("Value").Value,
                                  Run = Convert.ToBoolean(param.Attribute("Run").Value),
                                  Number = (int?) param.Attribute("Number"),
                                  Directory = param.Attribute("Directory").Value,
                                  Filename = (string)param.Attribute("Filename") ?? "None",
                                  Source = (string)param.Attribute("Source") ?? "None",
                                  FileTypes = (string)param.Attribute("FileTypes") ?? "None"
                              }).ToList();

X here is an XDocument, Parameter is a class with Name, Value, Run, etc members. x.Descendants gets you the children of the root node where they are a node named Parameter. You can access the attribute values, and set your member variables equal to them. This way you get a list of classes representing all the elements of a certain kind in your xml file, and can then spend less time parsing and more time being awesome.

phyllis diller
  • 795
  • 2
  • 9
  • 21
1

If you can get this DTD as a na XSD file then you should have a look at a tool that comes with .Net, XSD.Exe which will be in your Framework folder.

I think the format would be

XSD.exe /C myXsd.xsd

Here's the MSDN page for the tool: http://msdn.microsoft.com/en-us/library/x6c1kb0s%28VS.71%29.aspx

But basically, it creates classes based upon the schema definition.

Russ Clarke
  • 17,511
  • 4
  • 41
  • 45
1

If you convert your DTD to an XSD schema using this w3c tool, you can use xsd.exe or XML Sample Code Generator to create the classes.