简体   繁体   中英

Assign default values in Dataset from xml

I have this file:

<infNFe versao="2.00">
    <ide>
        <cUF>35</cUF>
        <cNF>10123856</cNF>
        ...
    </ide>
    <det nItem="1">
        <prod>
            ...
        </prod>
        <imposto>
            <ICMS>
                ...
            </ICMS>
            <IPI>
                <cEnq>999</cEnq>
                <IPITrib>
                    <CST>49</CST>
                    <vBC>29.40</vBC>
                    ...
                </IPITrib>
            </IPI>
        </imposto>
    </det>
    <det nItem="2">
        <prod>
            ...
        </prod>
        <imposto>
            <ICMS>
                ...
            </ICMS>
        </imposto>
    </det>
</infNFe>

Notice that the tag "IPI" doesn't exists within the second tag "det".

I need to populate a dataset with this file and ensure that all tags are filled.

I'm trying this:

DataSet ds = new DataSet();
ds.ReadXml(arquivo);

if (ds.Tables["IPI"] == null)
{
    ds.Tables.Add("IPI");
}
if (ds.Tables["IPITrib"] == null) {
    ds.Tables.Add("IPITrib");
}
if (ds.Tables["IPITrib"].Columns["vIPI"] == null)        
{
    ds.Tables["IPITrib"].Columns.Add("vIPI"); 
}

The second line remains without the tag because the tag "IPI" is contained in the first tag "det." - IMO

How do I fill the second record manually?

Although it is bit off topic, but I would suggest to use typed datasets with xml. One article about this is here: http://www.codeproject.com/KB/database/XmlToTypedDataSet.aspx

If you don't need datasets, even better way I think would be to use generated xsd to produce classes, from which you could serialize xml and vice versa, command line would look like:

xsd.exe your.xsd /classes

I think more about that is here: http://www.codeproject.com/KB/XML/Serialization.aspx

Things is, that with typed datasets/or serialization classes will be much easier to write and read code and you'll get your answer automagically :)

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