6

Hi is it possible to Create New Classes in C# ,classes which the Application Read's from XML and Declare they're Attributes also reading from XML . like :

<item id=1> <Name>John</Name> <Surname>Kennedy</Surname> <Age>24</Age> </item> 

bests.

4
  • do you mean actually create a new, never-before-seen, type, or just a new instance of an existing type? Commented Jun 3, 2011 at 8:13
  • if you are trying to create a class with the above xml, then it is woefully inadequate. For example, what would be the type of Age? int, uint, short, byte? Or do you want to store all fields as string? Commented Jun 3, 2011 at 12:20
  • Looks like you are trying to create an instance of the type item (Person) rather than creating a new type. Commented Jun 3, 2011 at 12:21
  • 1
    @Devendra D. Chavan the Question is more Abstract so no im not asking about the Person Item ,that was only a Sample ,and about data types if a Field Contains Only Numbers in all Cases i`ll Convert all Values to INT so this is not a Simple Task but a Problem which my Team will work on it in the Future and the Application is based on Artificial Intelligence . Commented Jun 3, 2011 at 14:51

6 Answers 6

10

Yes it is with System.Reflection.Emit namespace.

But in .net 4.0 you can use dynamic keywoard for this. Like this http://blogs.msdn.com/b/mcsuksoldev/archive/2010/02/04/dynamic-xml-reader-with-c-and-net-4-0.aspx

without dynamic, even if you create new class, you will need reflection to access their properties

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

1 Comment

I would also add that he doesn't probably need an entirely new class, and working with XML is usually done with LINQ to XML.
2

Yes it is.

Here you can find how to.

But instead of this you can also store this structure in a Map, where the key is and id and value is a other map that store the properties where key is the name and values is the value.

Comments

0

You can deserialize this XML in a new class! Try something like this:

 public static T DeserializeObject<T>(string filePath) { XmlDocument doc = new XmlDocument(); doc.Load(filePath); XmlNodeReader reader = new XmlNodeReader(doc.DocumentElement); XmlSerializer ser = new XmlSerializer(typeof(T)); object obj = ser.Deserialize(reader); return (T)obj; } 

3 Comments

He is not looking for creating new Objects. I think the structure of the XML file is not known in compile time. He is asking for creating new classes.
+1; I don't think it is clear from the original question whether the poster wants to create a new type, or just to deserialize xml to an existing type. I think the latter is more likely.
@ShellShock i don't know all Part of XML only few things but Nodes the User can name Whatever it wan't ,let say we have a Person with 4 Names !In that case i dont want to use String[] or Generics i need to declare 4 Strings like Name1 Name2 Name3 Name4 .
0

You have to use Reflection api for doing that which is a complicated thing. Please describe your scenario. Maybe there are simpler approaches.

Comments

0

here is a simple introduction for you http://blog.alxandr.me/2010/10/16/an-introduction-to-system-reflection-emit-and-opcodes/

have fun

Comments

-1

I doesn't really make sense to. Since C# is strongly typed, you're not going to know at compile type what the object looks like, so you're not going to be able to use it.

Maybe you're better off making an object that uses a Dictionary with the key being the node name. Then you can query the dictionary to find what properties it has.

1 Comment

Well my friend im planing to have my XML Criteria and User who want to use My Application should construct it's XML ,but the ISSUE is i don't want to Put a STRONG Criteria so the User will know what to put but where to put Values and Items would not be an issue if i can reach the GOAL IN MY Question ! @Ray

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.