C# - YAXLib: “No elements with this name found:”












1















I have two classes that are used for de/serializing an XML file.



XmlExampleBasicUnit.cs:



[YAXSerializeAs("Unit")]
public class XmlExampleBasicUnit
{
[YAXSerializeAs("StringVar")]
public String StringVar { get; set; }
[YAXSerializeAs("Int32Var")]
public Int32 Int32Var { get; set; }
[YAXSerializeAs("DoubleVar")]
public Double DoubleVar { get; set; }
[YAXSerializeAs("DateTimeVar")]
public String DateTimeVar { get; set; }
[YAXSerializeAs("CharVar")]
public String CharVar { get; set; }
}


XmlExampleCollectionOfUnits.cs:



[YAXSerializeAs("CollectionOfUnits")]
public class XmlExampleCollectionOfUnits
{
[YAXSerializeAs("Units")]
public List<XmlExampleBasicUnit> Units { get; set; }

public XmlExampleCollectionOfUnits(List<XmlExampleBasicUnit> units)
{
Units = units;
}

public XmlExampleCollectionOfUnits()
{
}
}


I'm trying to deserialize this file:



<?xml version="1.0" encoding="utf-8"?>
<CollectionOfUnits>
<Units>
<Unit>
<StringVar>TohleJeString</StringVar>
<Int32Var>24</Int32Var>
<DoubleVar>29.6</DoubleVar>
<DateTimeVar>17.11.1968</DateTimeVar>
<CharVar>c</CharVar>
</Unit>
<Unit>
<StringVar>TohleJeTakéString</StringVar>
<Int32Var>17</Int32Var>
<DoubleVar>5.9</DoubleVar>
<DateTimeVar>06.07.1415</DateTimeVar>
<CharVar>p</CharVar>
</Unit>
<Unit>
<StringVar>NoATohleTaké</StringVar>
<Int32Var>2</Int32Var>
<DoubleVar>78.5</DoubleVar>
<DateTimeVar>06.12.1774</DateTimeVar>
<CharVar>x</CharVar>
</Unit>
</Units>



It's all handled by this function:



public T Import<T>(String fileName) where T : class
{
YAXSerializer serializer = new YAXSerializer(typeof(T));
return (T)serializer.Deserialize(fileName);
}


When I run the code I get following error: No elements with this name found: './Units'. As you can see there already is an element called Units.










share|improve this question


















  • 1





    it doesn't give me any error ! how did you call Import function?

    – er-sho
    Nov 28 '18 at 10:38








  • 1





    Thank you! I have realized the Import() function is reading incorrect values.

    – Chyu
    Nov 28 '18 at 10:58











  • you need to pass xml content to Import function rather than passing path of xml file

    – er-sho
    Nov 28 '18 at 11:00











  • What if I want to use path of XML file?

    – Chyu
    Nov 28 '18 at 11:43











  • then you have to fetch content of xml in your Import function from path. let me add an solution for you

    – er-sho
    Nov 28 '18 at 11:51
















1















I have two classes that are used for de/serializing an XML file.



XmlExampleBasicUnit.cs:



[YAXSerializeAs("Unit")]
public class XmlExampleBasicUnit
{
[YAXSerializeAs("StringVar")]
public String StringVar { get; set; }
[YAXSerializeAs("Int32Var")]
public Int32 Int32Var { get; set; }
[YAXSerializeAs("DoubleVar")]
public Double DoubleVar { get; set; }
[YAXSerializeAs("DateTimeVar")]
public String DateTimeVar { get; set; }
[YAXSerializeAs("CharVar")]
public String CharVar { get; set; }
}


XmlExampleCollectionOfUnits.cs:



[YAXSerializeAs("CollectionOfUnits")]
public class XmlExampleCollectionOfUnits
{
[YAXSerializeAs("Units")]
public List<XmlExampleBasicUnit> Units { get; set; }

public XmlExampleCollectionOfUnits(List<XmlExampleBasicUnit> units)
{
Units = units;
}

public XmlExampleCollectionOfUnits()
{
}
}


I'm trying to deserialize this file:



<?xml version="1.0" encoding="utf-8"?>
<CollectionOfUnits>
<Units>
<Unit>
<StringVar>TohleJeString</StringVar>
<Int32Var>24</Int32Var>
<DoubleVar>29.6</DoubleVar>
<DateTimeVar>17.11.1968</DateTimeVar>
<CharVar>c</CharVar>
</Unit>
<Unit>
<StringVar>TohleJeTakéString</StringVar>
<Int32Var>17</Int32Var>
<DoubleVar>5.9</DoubleVar>
<DateTimeVar>06.07.1415</DateTimeVar>
<CharVar>p</CharVar>
</Unit>
<Unit>
<StringVar>NoATohleTaké</StringVar>
<Int32Var>2</Int32Var>
<DoubleVar>78.5</DoubleVar>
<DateTimeVar>06.12.1774</DateTimeVar>
<CharVar>x</CharVar>
</Unit>
</Units>



It's all handled by this function:



public T Import<T>(String fileName) where T : class
{
YAXSerializer serializer = new YAXSerializer(typeof(T));
return (T)serializer.Deserialize(fileName);
}


When I run the code I get following error: No elements with this name found: './Units'. As you can see there already is an element called Units.










share|improve this question


















  • 1





    it doesn't give me any error ! how did you call Import function?

    – er-sho
    Nov 28 '18 at 10:38








  • 1





    Thank you! I have realized the Import() function is reading incorrect values.

    – Chyu
    Nov 28 '18 at 10:58











  • you need to pass xml content to Import function rather than passing path of xml file

    – er-sho
    Nov 28 '18 at 11:00











  • What if I want to use path of XML file?

    – Chyu
    Nov 28 '18 at 11:43











  • then you have to fetch content of xml in your Import function from path. let me add an solution for you

    – er-sho
    Nov 28 '18 at 11:51














1












1








1








I have two classes that are used for de/serializing an XML file.



XmlExampleBasicUnit.cs:



[YAXSerializeAs("Unit")]
public class XmlExampleBasicUnit
{
[YAXSerializeAs("StringVar")]
public String StringVar { get; set; }
[YAXSerializeAs("Int32Var")]
public Int32 Int32Var { get; set; }
[YAXSerializeAs("DoubleVar")]
public Double DoubleVar { get; set; }
[YAXSerializeAs("DateTimeVar")]
public String DateTimeVar { get; set; }
[YAXSerializeAs("CharVar")]
public String CharVar { get; set; }
}


XmlExampleCollectionOfUnits.cs:



[YAXSerializeAs("CollectionOfUnits")]
public class XmlExampleCollectionOfUnits
{
[YAXSerializeAs("Units")]
public List<XmlExampleBasicUnit> Units { get; set; }

public XmlExampleCollectionOfUnits(List<XmlExampleBasicUnit> units)
{
Units = units;
}

public XmlExampleCollectionOfUnits()
{
}
}


I'm trying to deserialize this file:



<?xml version="1.0" encoding="utf-8"?>
<CollectionOfUnits>
<Units>
<Unit>
<StringVar>TohleJeString</StringVar>
<Int32Var>24</Int32Var>
<DoubleVar>29.6</DoubleVar>
<DateTimeVar>17.11.1968</DateTimeVar>
<CharVar>c</CharVar>
</Unit>
<Unit>
<StringVar>TohleJeTakéString</StringVar>
<Int32Var>17</Int32Var>
<DoubleVar>5.9</DoubleVar>
<DateTimeVar>06.07.1415</DateTimeVar>
<CharVar>p</CharVar>
</Unit>
<Unit>
<StringVar>NoATohleTaké</StringVar>
<Int32Var>2</Int32Var>
<DoubleVar>78.5</DoubleVar>
<DateTimeVar>06.12.1774</DateTimeVar>
<CharVar>x</CharVar>
</Unit>
</Units>



It's all handled by this function:



public T Import<T>(String fileName) where T : class
{
YAXSerializer serializer = new YAXSerializer(typeof(T));
return (T)serializer.Deserialize(fileName);
}


When I run the code I get following error: No elements with this name found: './Units'. As you can see there already is an element called Units.










share|improve this question














I have two classes that are used for de/serializing an XML file.



XmlExampleBasicUnit.cs:



[YAXSerializeAs("Unit")]
public class XmlExampleBasicUnit
{
[YAXSerializeAs("StringVar")]
public String StringVar { get; set; }
[YAXSerializeAs("Int32Var")]
public Int32 Int32Var { get; set; }
[YAXSerializeAs("DoubleVar")]
public Double DoubleVar { get; set; }
[YAXSerializeAs("DateTimeVar")]
public String DateTimeVar { get; set; }
[YAXSerializeAs("CharVar")]
public String CharVar { get; set; }
}


XmlExampleCollectionOfUnits.cs:



[YAXSerializeAs("CollectionOfUnits")]
public class XmlExampleCollectionOfUnits
{
[YAXSerializeAs("Units")]
public List<XmlExampleBasicUnit> Units { get; set; }

public XmlExampleCollectionOfUnits(List<XmlExampleBasicUnit> units)
{
Units = units;
}

public XmlExampleCollectionOfUnits()
{
}
}


I'm trying to deserialize this file:



<?xml version="1.0" encoding="utf-8"?>
<CollectionOfUnits>
<Units>
<Unit>
<StringVar>TohleJeString</StringVar>
<Int32Var>24</Int32Var>
<DoubleVar>29.6</DoubleVar>
<DateTimeVar>17.11.1968</DateTimeVar>
<CharVar>c</CharVar>
</Unit>
<Unit>
<StringVar>TohleJeTakéString</StringVar>
<Int32Var>17</Int32Var>
<DoubleVar>5.9</DoubleVar>
<DateTimeVar>06.07.1415</DateTimeVar>
<CharVar>p</CharVar>
</Unit>
<Unit>
<StringVar>NoATohleTaké</StringVar>
<Int32Var>2</Int32Var>
<DoubleVar>78.5</DoubleVar>
<DateTimeVar>06.12.1774</DateTimeVar>
<CharVar>x</CharVar>
</Unit>
</Units>



It's all handled by this function:



public T Import<T>(String fileName) where T : class
{
YAXSerializer serializer = new YAXSerializer(typeof(T));
return (T)serializer.Deserialize(fileName);
}


When I run the code I get following error: No elements with this name found: './Units'. As you can see there already is an element called Units.







c# xml xml-serialization yaxlib






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 28 '18 at 10:20









ChyuChyu

157317




157317








  • 1





    it doesn't give me any error ! how did you call Import function?

    – er-sho
    Nov 28 '18 at 10:38








  • 1





    Thank you! I have realized the Import() function is reading incorrect values.

    – Chyu
    Nov 28 '18 at 10:58











  • you need to pass xml content to Import function rather than passing path of xml file

    – er-sho
    Nov 28 '18 at 11:00











  • What if I want to use path of XML file?

    – Chyu
    Nov 28 '18 at 11:43











  • then you have to fetch content of xml in your Import function from path. let me add an solution for you

    – er-sho
    Nov 28 '18 at 11:51














  • 1





    it doesn't give me any error ! how did you call Import function?

    – er-sho
    Nov 28 '18 at 10:38








  • 1





    Thank you! I have realized the Import() function is reading incorrect values.

    – Chyu
    Nov 28 '18 at 10:58











  • you need to pass xml content to Import function rather than passing path of xml file

    – er-sho
    Nov 28 '18 at 11:00











  • What if I want to use path of XML file?

    – Chyu
    Nov 28 '18 at 11:43











  • then you have to fetch content of xml in your Import function from path. let me add an solution for you

    – er-sho
    Nov 28 '18 at 11:51








1




1





it doesn't give me any error ! how did you call Import function?

– er-sho
Nov 28 '18 at 10:38







it doesn't give me any error ! how did you call Import function?

– er-sho
Nov 28 '18 at 10:38






1




1





Thank you! I have realized the Import() function is reading incorrect values.

– Chyu
Nov 28 '18 at 10:58





Thank you! I have realized the Import() function is reading incorrect values.

– Chyu
Nov 28 '18 at 10:58













you need to pass xml content to Import function rather than passing path of xml file

– er-sho
Nov 28 '18 at 11:00





you need to pass xml content to Import function rather than passing path of xml file

– er-sho
Nov 28 '18 at 11:00













What if I want to use path of XML file?

– Chyu
Nov 28 '18 at 11:43





What if I want to use path of XML file?

– Chyu
Nov 28 '18 at 11:43













then you have to fetch content of xml in your Import function from path. let me add an solution for you

– er-sho
Nov 28 '18 at 11:51





then you have to fetch content of xml in your Import function from path. let me add an solution for you

– er-sho
Nov 28 '18 at 11:51












1 Answer
1






active

oldest

votes


















1














Read all your xml content from your path by using File.ReadAllText(fileName) and pass it to Deserialize method like.



public static T Import<T>(String fileName) where T : class
{
string xmlData = File.ReadAllText(fileName); //Read xml content from path
YAXSerializer serializer = new YAXSerializer(typeof(T));
return (T)serializer.Deserialize(xmlData); //Pass xml content to Deserialize.
}





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%2f53517120%2fc-sharp-yaxlib-no-elements-with-this-name-found%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









    1














    Read all your xml content from your path by using File.ReadAllText(fileName) and pass it to Deserialize method like.



    public static T Import<T>(String fileName) where T : class
    {
    string xmlData = File.ReadAllText(fileName); //Read xml content from path
    YAXSerializer serializer = new YAXSerializer(typeof(T));
    return (T)serializer.Deserialize(xmlData); //Pass xml content to Deserialize.
    }





    share|improve this answer




























      1














      Read all your xml content from your path by using File.ReadAllText(fileName) and pass it to Deserialize method like.



      public static T Import<T>(String fileName) where T : class
      {
      string xmlData = File.ReadAllText(fileName); //Read xml content from path
      YAXSerializer serializer = new YAXSerializer(typeof(T));
      return (T)serializer.Deserialize(xmlData); //Pass xml content to Deserialize.
      }





      share|improve this answer


























        1












        1








        1







        Read all your xml content from your path by using File.ReadAllText(fileName) and pass it to Deserialize method like.



        public static T Import<T>(String fileName) where T : class
        {
        string xmlData = File.ReadAllText(fileName); //Read xml content from path
        YAXSerializer serializer = new YAXSerializer(typeof(T));
        return (T)serializer.Deserialize(xmlData); //Pass xml content to Deserialize.
        }





        share|improve this answer













        Read all your xml content from your path by using File.ReadAllText(fileName) and pass it to Deserialize method like.



        public static T Import<T>(String fileName) where T : class
        {
        string xmlData = File.ReadAllText(fileName); //Read xml content from path
        YAXSerializer serializer = new YAXSerializer(typeof(T));
        return (T)serializer.Deserialize(xmlData); //Pass xml content to Deserialize.
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 28 '18 at 13:35









        er-shoer-sho

        6,4752619




        6,4752619
































            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%2f53517120%2fc-sharp-yaxlib-no-elements-with-this-name-found%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

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)