How to make a general type that can be xml, json or htmlHow do I format a Microsoft JSON date?Can comments be used in JSON?How can I pretty-print JSON in a shell script?What is the correct JSON content type?Why is it a bad practice to return generated HTML instead of JSON? Or is it?How do I generate a random int number?How Do You Parse and Process HTML/XML in PHP?How can I pretty-print JSON using JavaScript?How do I POST JSON data with Curl from a terminal/commandline to Test Spring REST?How do I get ASP.NET Web API to return JSON instead of XML using Chrome?

What killed these X2 caps?

Could gravitational lensing be used to protect a spaceship from a laser?

Reserved de-dupe rules

Increase size of symbol intercal when in superscript position

Western buddy movie with a supernatural twist where a woman turns into an eagle at the end

Is it legal for company to use my work email to pretend I still work there?

Why is it a bad idea to hire a hitman to eliminate most corrupt politicians?

How to show the equivalence between the regularized regression and their constraint formulas using KKT

Why is the ratio of two extensive quantities always intensive?

Why "Having chlorophyll without photosynthesis is actually very dangerous" and "like living with a bomb"?

How to draw the figure with four pentagons?

Infinite Abelian subgroup of infinite non Abelian group example

What to put in ESTA if staying in US for a few days before going on to Canada

Should I tell management that I intend to leave due to bad software development practices?

How do conventional missiles fly?

Python: return float 1.0 as int 1 but float 1.5 as float 1.5

Why does Kotter return in Welcome Back Kotter

Does a druid starting with a bow start with no arrows?

How do I find out when a node was added to an availability group?

Can a virus destroy the BIOS of a modern computer?

Can I ask the recruiters in my resume to put the reason why I am rejected?

What reasons are there for a Capitalist to oppose a 100% inheritance tax?

What is going on with Captain Marvel's blood colour?

Twin primes whose sum is a cube



How to make a general type that can be xml, json or html


How do I format a Microsoft JSON date?Can comments be used in JSON?How can I pretty-print JSON in a shell script?What is the correct JSON content type?Why is it a bad practice to return generated HTML instead of JSON? Or is it?How do I generate a random int number?How Do You Parse and Process HTML/XML in PHP?How can I pretty-print JSON using JavaScript?How do I POST JSON data with Curl from a terminal/commandline to Test Spring REST?How do I get ASP.NET Web API to return JSON instead of XML using Chrome?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I have a program that at some point will load a text file. This text file's content will either be in xml, json or html format.



My current approach looks like this:



  1. Load the file, parse it, check if it's json,

    Yes: save it to a predefined string,

    No:

  2. Load the file, parse it, check if it's xml,

    Yes: save it to a predefined XmlDocument,

    No:

  3. Load the file, parse it, check if it's html,

    Yes: save it to a predefined HtmlDocument,

    No: Error

And in the end I need to distinguish. Check all of them. And whichever is not null will be further processed.



Also I had to instantiate the XmlDocument and the HtmlDocument like this:



XmlDocument XmlDoc = new XmlDocument();
XmlDoc = null;,


to make sure they're actually null when distinguishing in the end. Because if I only construct, they won't be null.



This looks very dirty. I know I can do something with interfaces and maybe a factory. But what confuses me is: Say I build an Interface IDocument, doesn't the content of this thing have to be generic? I mean I have different file types.. I'm very confused.










share|improve this question
























  • It depends on the processing that is performed on each of these different inputs. Is your processing of json different to xml? Maybe the processing code should be lumped with the check type and parse steps, then your interface would expose the processed files, not the raw file

    – strongbutgood
    Mar 21 at 22:13











  • Just a thought on your content. All the content of the files are just string, unless its binary serialized. So get out the content of the file, try to parse it as the different types. that way, you only load the file once, but tryparse multiple times :)

    – VisualBean
    Mar 21 at 22:14











  • Yes it is different. I’m performing the same action on them (updating a value depending on a xpath/jsonpath. The logic is pretty much the same but oviobviously the way I manipulate each is different.

    – Marcel B
    Mar 21 at 23:19











  • I'm wondering if do you have an option to alter your program to accept .xml , .json, .html file instead of just .text file? That might be easier to handle than allowing to load a txt file that contains those different format.

    – jtabuloc
    Mar 22 at 3:29












  • Can you use MIME type to determine which type of file is being loaded? If so, I would create a factory that uses MIME type to pick the appropriate class to load and manipulate the data. So, 4 classes. 1 for parsing/manipulating each type of file + 1 factory.

    – aridlehoover
    Mar 22 at 6:17

















0















I have a program that at some point will load a text file. This text file's content will either be in xml, json or html format.



My current approach looks like this:



  1. Load the file, parse it, check if it's json,

    Yes: save it to a predefined string,

    No:

  2. Load the file, parse it, check if it's xml,

    Yes: save it to a predefined XmlDocument,

    No:

  3. Load the file, parse it, check if it's html,

    Yes: save it to a predefined HtmlDocument,

    No: Error

And in the end I need to distinguish. Check all of them. And whichever is not null will be further processed.



Also I had to instantiate the XmlDocument and the HtmlDocument like this:



XmlDocument XmlDoc = new XmlDocument();
XmlDoc = null;,


to make sure they're actually null when distinguishing in the end. Because if I only construct, they won't be null.



This looks very dirty. I know I can do something with interfaces and maybe a factory. But what confuses me is: Say I build an Interface IDocument, doesn't the content of this thing have to be generic? I mean I have different file types.. I'm very confused.










share|improve this question
























  • It depends on the processing that is performed on each of these different inputs. Is your processing of json different to xml? Maybe the processing code should be lumped with the check type and parse steps, then your interface would expose the processed files, not the raw file

    – strongbutgood
    Mar 21 at 22:13











  • Just a thought on your content. All the content of the files are just string, unless its binary serialized. So get out the content of the file, try to parse it as the different types. that way, you only load the file once, but tryparse multiple times :)

    – VisualBean
    Mar 21 at 22:14











  • Yes it is different. I’m performing the same action on them (updating a value depending on a xpath/jsonpath. The logic is pretty much the same but oviobviously the way I manipulate each is different.

    – Marcel B
    Mar 21 at 23:19











  • I'm wondering if do you have an option to alter your program to accept .xml , .json, .html file instead of just .text file? That might be easier to handle than allowing to load a txt file that contains those different format.

    – jtabuloc
    Mar 22 at 3:29












  • Can you use MIME type to determine which type of file is being loaded? If so, I would create a factory that uses MIME type to pick the appropriate class to load and manipulate the data. So, 4 classes. 1 for parsing/manipulating each type of file + 1 factory.

    – aridlehoover
    Mar 22 at 6:17













0












0








0








I have a program that at some point will load a text file. This text file's content will either be in xml, json or html format.



My current approach looks like this:



  1. Load the file, parse it, check if it's json,

    Yes: save it to a predefined string,

    No:

  2. Load the file, parse it, check if it's xml,

    Yes: save it to a predefined XmlDocument,

    No:

  3. Load the file, parse it, check if it's html,

    Yes: save it to a predefined HtmlDocument,

    No: Error

And in the end I need to distinguish. Check all of them. And whichever is not null will be further processed.



Also I had to instantiate the XmlDocument and the HtmlDocument like this:



XmlDocument XmlDoc = new XmlDocument();
XmlDoc = null;,


to make sure they're actually null when distinguishing in the end. Because if I only construct, they won't be null.



This looks very dirty. I know I can do something with interfaces and maybe a factory. But what confuses me is: Say I build an Interface IDocument, doesn't the content of this thing have to be generic? I mean I have different file types.. I'm very confused.










share|improve this question
















I have a program that at some point will load a text file. This text file's content will either be in xml, json or html format.



My current approach looks like this:



  1. Load the file, parse it, check if it's json,

    Yes: save it to a predefined string,

    No:

  2. Load the file, parse it, check if it's xml,

    Yes: save it to a predefined XmlDocument,

    No:

  3. Load the file, parse it, check if it's html,

    Yes: save it to a predefined HtmlDocument,

    No: Error

And in the end I need to distinguish. Check all of them. And whichever is not null will be further processed.



Also I had to instantiate the XmlDocument and the HtmlDocument like this:



XmlDocument XmlDoc = new XmlDocument();
XmlDoc = null;,


to make sure they're actually null when distinguishing in the end. Because if I only construct, they won't be null.



This looks very dirty. I know I can do something with interfaces and maybe a factory. But what confuses me is: Say I build an Interface IDocument, doesn't the content of this thing have to be generic? I mean I have different file types.. I'm very confused.







c# json xml oop






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 21 at 22:00









Cody Gray

195k35383475




195k35383475










asked Mar 21 at 21:53









Marcel BMarcel B

655




655












  • It depends on the processing that is performed on each of these different inputs. Is your processing of json different to xml? Maybe the processing code should be lumped with the check type and parse steps, then your interface would expose the processed files, not the raw file

    – strongbutgood
    Mar 21 at 22:13











  • Just a thought on your content. All the content of the files are just string, unless its binary serialized. So get out the content of the file, try to parse it as the different types. that way, you only load the file once, but tryparse multiple times :)

    – VisualBean
    Mar 21 at 22:14











  • Yes it is different. I’m performing the same action on them (updating a value depending on a xpath/jsonpath. The logic is pretty much the same but oviobviously the way I manipulate each is different.

    – Marcel B
    Mar 21 at 23:19











  • I'm wondering if do you have an option to alter your program to accept .xml , .json, .html file instead of just .text file? That might be easier to handle than allowing to load a txt file that contains those different format.

    – jtabuloc
    Mar 22 at 3:29












  • Can you use MIME type to determine which type of file is being loaded? If so, I would create a factory that uses MIME type to pick the appropriate class to load and manipulate the data. So, 4 classes. 1 for parsing/manipulating each type of file + 1 factory.

    – aridlehoover
    Mar 22 at 6:17

















  • It depends on the processing that is performed on each of these different inputs. Is your processing of json different to xml? Maybe the processing code should be lumped with the check type and parse steps, then your interface would expose the processed files, not the raw file

    – strongbutgood
    Mar 21 at 22:13











  • Just a thought on your content. All the content of the files are just string, unless its binary serialized. So get out the content of the file, try to parse it as the different types. that way, you only load the file once, but tryparse multiple times :)

    – VisualBean
    Mar 21 at 22:14











  • Yes it is different. I’m performing the same action on them (updating a value depending on a xpath/jsonpath. The logic is pretty much the same but oviobviously the way I manipulate each is different.

    – Marcel B
    Mar 21 at 23:19











  • I'm wondering if do you have an option to alter your program to accept .xml , .json, .html file instead of just .text file? That might be easier to handle than allowing to load a txt file that contains those different format.

    – jtabuloc
    Mar 22 at 3:29












  • Can you use MIME type to determine which type of file is being loaded? If so, I would create a factory that uses MIME type to pick the appropriate class to load and manipulate the data. So, 4 classes. 1 for parsing/manipulating each type of file + 1 factory.

    – aridlehoover
    Mar 22 at 6:17
















It depends on the processing that is performed on each of these different inputs. Is your processing of json different to xml? Maybe the processing code should be lumped with the check type and parse steps, then your interface would expose the processed files, not the raw file

– strongbutgood
Mar 21 at 22:13





It depends on the processing that is performed on each of these different inputs. Is your processing of json different to xml? Maybe the processing code should be lumped with the check type and parse steps, then your interface would expose the processed files, not the raw file

– strongbutgood
Mar 21 at 22:13













Just a thought on your content. All the content of the files are just string, unless its binary serialized. So get out the content of the file, try to parse it as the different types. that way, you only load the file once, but tryparse multiple times :)

– VisualBean
Mar 21 at 22:14





Just a thought on your content. All the content of the files are just string, unless its binary serialized. So get out the content of the file, try to parse it as the different types. that way, you only load the file once, but tryparse multiple times :)

– VisualBean
Mar 21 at 22:14













Yes it is different. I’m performing the same action on them (updating a value depending on a xpath/jsonpath. The logic is pretty much the same but oviobviously the way I manipulate each is different.

– Marcel B
Mar 21 at 23:19





Yes it is different. I’m performing the same action on them (updating a value depending on a xpath/jsonpath. The logic is pretty much the same but oviobviously the way I manipulate each is different.

– Marcel B
Mar 21 at 23:19













I'm wondering if do you have an option to alter your program to accept .xml , .json, .html file instead of just .text file? That might be easier to handle than allowing to load a txt file that contains those different format.

– jtabuloc
Mar 22 at 3:29






I'm wondering if do you have an option to alter your program to accept .xml , .json, .html file instead of just .text file? That might be easier to handle than allowing to load a txt file that contains those different format.

– jtabuloc
Mar 22 at 3:29














Can you use MIME type to determine which type of file is being loaded? If so, I would create a factory that uses MIME type to pick the appropriate class to load and manipulate the data. So, 4 classes. 1 for parsing/manipulating each type of file + 1 factory.

– aridlehoover
Mar 22 at 6:17





Can you use MIME type to determine which type of file is being loaded? If so, I would create a factory that uses MIME type to pick the appropriate class to load and manipulate the data. So, 4 classes. 1 for parsing/manipulating each type of file + 1 factory.

– aridlehoover
Mar 22 at 6:17












1 Answer
1






active

oldest

votes


















0














Well you should still check for these 3 types, but however you can create your own class and implement these checking and parsing inside your own class (or interface):



You can create an implicit casting for all three types so that you can set its value with any of these 3 types:



public class MultiDoc

private XmlDocument xml = new XmlDocument();
private string type = "";
public void Load(string content)

//do your checking to match the types here


public T Get<T>()

if(typeof(T) == typeof(XmlDocument))
return xml;
elseif ...


//implicit casting type XmlDocument
public MultiDoc(XmlDocument input) xml = input; type = "xml";
public static implicit operator MultiDoc(XmlDocument input) return new MultiDoc(input);

//do same casting for other types.




Now Becuase of implicit casting you can simply do:



XmlDocument somexml = ....
MultiDoc mc = somexml; //implicity cast XmlDocument to MultiDoc


And for getting:



XmlDocument somexml = mc.Get<XmlDocument>();





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55289828%2fhow-to-make-a-general-type-that-can-be-xml-json-or-html%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Well you should still check for these 3 types, but however you can create your own class and implement these checking and parsing inside your own class (or interface):



    You can create an implicit casting for all three types so that you can set its value with any of these 3 types:



    public class MultiDoc

    private XmlDocument xml = new XmlDocument();
    private string type = "";
    public void Load(string content)

    //do your checking to match the types here


    public T Get<T>()

    if(typeof(T) == typeof(XmlDocument))
    return xml;
    elseif ...


    //implicit casting type XmlDocument
    public MultiDoc(XmlDocument input) xml = input; type = "xml";
    public static implicit operator MultiDoc(XmlDocument input) return new MultiDoc(input);

    //do same casting for other types.




    Now Becuase of implicit casting you can simply do:



    XmlDocument somexml = ....
    MultiDoc mc = somexml; //implicity cast XmlDocument to MultiDoc


    And for getting:



    XmlDocument somexml = mc.Get<XmlDocument>();





    share|improve this answer





























      0














      Well you should still check for these 3 types, but however you can create your own class and implement these checking and parsing inside your own class (or interface):



      You can create an implicit casting for all three types so that you can set its value with any of these 3 types:



      public class MultiDoc

      private XmlDocument xml = new XmlDocument();
      private string type = "";
      public void Load(string content)

      //do your checking to match the types here


      public T Get<T>()

      if(typeof(T) == typeof(XmlDocument))
      return xml;
      elseif ...


      //implicit casting type XmlDocument
      public MultiDoc(XmlDocument input) xml = input; type = "xml";
      public static implicit operator MultiDoc(XmlDocument input) return new MultiDoc(input);

      //do same casting for other types.




      Now Becuase of implicit casting you can simply do:



      XmlDocument somexml = ....
      MultiDoc mc = somexml; //implicity cast XmlDocument to MultiDoc


      And for getting:



      XmlDocument somexml = mc.Get<XmlDocument>();





      share|improve this answer



























        0












        0








        0







        Well you should still check for these 3 types, but however you can create your own class and implement these checking and parsing inside your own class (or interface):



        You can create an implicit casting for all three types so that you can set its value with any of these 3 types:



        public class MultiDoc

        private XmlDocument xml = new XmlDocument();
        private string type = "";
        public void Load(string content)

        //do your checking to match the types here


        public T Get<T>()

        if(typeof(T) == typeof(XmlDocument))
        return xml;
        elseif ...


        //implicit casting type XmlDocument
        public MultiDoc(XmlDocument input) xml = input; type = "xml";
        public static implicit operator MultiDoc(XmlDocument input) return new MultiDoc(input);

        //do same casting for other types.




        Now Becuase of implicit casting you can simply do:



        XmlDocument somexml = ....
        MultiDoc mc = somexml; //implicity cast XmlDocument to MultiDoc


        And for getting:



        XmlDocument somexml = mc.Get<XmlDocument>();





        share|improve this answer















        Well you should still check for these 3 types, but however you can create your own class and implement these checking and parsing inside your own class (or interface):



        You can create an implicit casting for all three types so that you can set its value with any of these 3 types:



        public class MultiDoc

        private XmlDocument xml = new XmlDocument();
        private string type = "";
        public void Load(string content)

        //do your checking to match the types here


        public T Get<T>()

        if(typeof(T) == typeof(XmlDocument))
        return xml;
        elseif ...


        //implicit casting type XmlDocument
        public MultiDoc(XmlDocument input) xml = input; type = "xml";
        public static implicit operator MultiDoc(XmlDocument input) return new MultiDoc(input);

        //do same casting for other types.




        Now Becuase of implicit casting you can simply do:



        XmlDocument somexml = ....
        MultiDoc mc = somexml; //implicity cast XmlDocument to MultiDoc


        And for getting:



        XmlDocument somexml = mc.Get<XmlDocument>();






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 21 at 22:41

























        answered Mar 21 at 22:30









        Ashkan Mobayen KhiabaniAshkan Mobayen Khiabani

        22.9k1768124




        22.9k1768124





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55289828%2fhow-to-make-a-general-type-that-can-be-xml-json-or-html%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

            155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해