简体   繁体   中英

how Deserializing this type of xml file?

<row>
    <id>1</id>
    <code></code>
    <name></name>
    <address></address>
    <state></state>
    <zone>?</zone>
</row>
<row>
    <id>2</id>
    <code>AA</code>
    <name>Ataria</name>
    <address>Sitapur National Highway 24, Uttar Pradesh</address>
    <state>Uttar Pradesh</state>
    <zone>NER</zone>
</row>

i have no root element in this xml file only row element start and end this xml file. how Deserializing this type of data ? in c#

If you sure that missing root is only the one issue with your XML - just add it manually:

string fileContent = File.ReadAllText(path);
string rawXml = "<root>" + fileContent + "</root>";

// now you can use LINQ-to-XML or whatever
XDocument xdoc = XDocument.Load(rawXml);

You can also load an XML Fragment directly, via

XmlReaderSettings settings = new XmlReaderSettings();
settings.ConformanceLevel = ConformanceLevel.Fragment;

using (XmlReader reader = XmlReader.Create("tracelog.xml", settings))
{
    while (reader.Read())
    {
        // Process each node of the fragment,
        // possibly using reader.ReadSubtree()
    }
}

You would create XElement s by passing the results of reader.ReadSubTree() to XElement.Load(...) .

Well to start with, it's not an XML file - or at least, it doesn't represent an XML document.

One option would be to copy the file into a new file which does have document start/end tags... then you can load it as a normal document. Just create a file, write a document start tag, copy the contents of this file, then write a document end tag, and close the file handle. You could even do this in memory.

Alternatively, it may be possible to read it as it is, in fragments - possibly via XmlReader . I can't say it's something I've done, and I'd generally encourage you to create a full XML file instead, as then you'll be on more familiar territory.

its not an XML file if it doesn't have the root. parser will throw an error if you try to parse it. you can do this way

<?xml version="1.0"?>
<Root>
--- add your file content here
</Root>  

then give this file path to the parser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM