0

i just trying to read a text file without some texts, as a example the XML file which I should read looks like this,

<?xml version="1.0"?>
<ProfileModel xmlns:xsi="test" xmlns:xsd="http://test">
  <Repositories>
    <BaseSettingsModel xsi:type="Server">
      <Name>test server</Name>
      <IsActive>false</IsActive>
      <IsHidden>false</IsHidden>
      <ServerRepository>https://test.com:4443/</ServerRepository>
    </BaseSettingsModel>
    <BaseSettingsModel xsi:type="FileSystem">
      <Name>Test AAA</Name>
      <IsActive>false</IsActive>
      <IsHidden>false</IsHidden>
      <FilesRepository>D:\Office\Templates</FilesRepository>
      <FormFilesRepository>D:\Office\Forms</FormFilesRepository>
    </BaseSettingsModel>    
  </Repositories>
  <KeyValues />
  <OutputMessageType>All</OutputMessageType>  
</ProfileModel>

So I just wanted to know how to read this kind of file without this xsi:type="Server" so if anyone could answer, it will be really helpful.

xodoheh
  • 1
  • 1
  • look at this Attribute https://stackoverflow.com/questions/29085381/mapping-xml-to-classes-in-c-sharp – daremachine Feb 09 '21 at 12:27
  • 1
    Why??? The xsi:type was added because you have a base class with two inherited classes Server and FileSystem and the attribute is needed to deserialize the results. – jdweng Feb 09 '21 at 12:28
  • You question should really be "Why didn't my deserialization work". Not propose a wrong solution. See : https://learn.microsoft.com/en-us/dotnet/api/system.xml.serialization.xmlincludeattribute?view=net-5.0 – jdweng Feb 09 '21 at 12:58
  • You have shown us your XML but you haven't shown us your deserialization code or data model so it's unclear where you are going wrong. You say you are reading a "text file" but are you reading it using `XmlSerializer`? `DataContractSerializer`? `File.ReadAllText()`? If you are using `XmlSerializer` them maybe [xsi:type attribute messing up C# XML deserialization](https://stackoverflow.com/a/36365689/3744182) and/or [Deserialize XML with multiple types](https://stackoverflow.com/q/50484173/3744182) answer your question. – dbc Feb 09 '21 at 13:35
  • 1
    `xmlns:xsi="test" xmlns:xsd="http://test` -- did you manually edit the values of these two attributes when you posted your question? These attributes have standard values, which are `xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"` – dbc Feb 09 '21 at 14:22
  • You want X. You thought “read this kind of file without this” would X. Now you discovered that didn't X. [We can't help you until you explain what X is](https://meta.stackexchange.com/questions/66377/). – Dour High Arch Feb 09 '21 at 18:50

0 Answers0