XElement Generates names starting with "<at:"

63 views Asked by At

I have problems when splitting large XML files. When I generate new elements (copying from the original file) to be inserted in the new, small file, they all starts with <at:

So for example, my root element looks like this: <at:export

Instead of this: <export

They have the same encoding (UTF-8) with Unix LF.

Anyone that knows why?

Edit: The code in my split function

public List<XDocument> SplitXML(string sourceFile, string chunkNode, int chunkSize)
    {
        var smallXMLs = new List<XDocument>();

        Console.WriteLine("Start reading XML document...");
        XDocument document = XDocument.Load(sourceFile);

        XElement content = (XElement)document.Root.LastNode;


        Console.WriteLine("Generating chunk docs...");
        var ids = content.Elements();
        XDocument newRequest = null;
        XElement root = null;
        XElement items = null;
        int i = 1;
        do
        {
            root = new XElement(document.Root.Name, document.Root.Attributes());
            items = new XElement(chunkNode);
            foreach (XElement myElement in document.Root.Nodes())
            {
                if (myElement.Name.LocalName != chunkNode)
                    root.Add(myElement);

            }
            newRequest = new XDocument();
            newRequest.Add(root);

            var chunk = ids.Take(chunkSize);
            items.Add(chunk);
            newRequest.Root.Add(items);
            chunk.Remove();
            smallXMLs.Add(newRequest);

            i++;
        } while (ids.Any());

        Console.WriteLine("Document {0} generated", i.ToString());

        return smallXMLs;
    }

Part of the source XML:

<?xml version='1.0' encoding='UTF-8'?>
<export xsi:schemaLocation="http://www.arcticgroup.se/tariff/arctictariff/export Measure.xsd " 
            xmlns="http://www.arcticgroup.se/tariff/arctictariff/export" 
            xmlns:at="http://www.arcticgroup.se/tariff/arctictariff/export" 
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <id>98f8050f-97fb-4c73-bf89-2a332ccc0715</id>
    <exportType>MeasureObject</exportType>
    <parameters>
        <queryDateStart>2005-01-01</queryDateStart>
        <queryDateEnd>2020-01-01</queryDateEnd>
    </parameters>
    <items>
        <measure at:geographicalAreaId="GCCG" at:goodsNomenclatureCode="1209220000" at:SIDGoodsNomenclature="32175" at:measureType="SEVAX" at:national="1" at:regulationId="1VAXTSKY" at:regulationRoleType="1" at:SID="-31825" at:SIDGeographicalArea="-10006" at:dateStart="2016-08-21" at:stoppedFlag="0" at:changeType="U">
            <measureCondition at:actionCode="01" at:certificateCode="006" at:certificateType="9" at:conditionCodeId="B" at:expression="[0.0,AMOUNT(0.0,&quot;SEK&quot;),TX]" at:national="1" at:sequenceNumber="001" at:SID="-17892">
            </measureCondition>
            <measureCondition at:actionCode="01" at:conditionCodeId="B" at:expression="$Base1 = ?KGM; $Rate = AMOUNT(0.09,&quot;SEK&quot;); $CalculatedResult = $Base1 * $Rate; $BaseLow = AMOUNT(1200.0,&quot;SEK&quot;); AMAX([$BaseLow, 1.0,VA],[$CalculatedResult, 1.0,VA])" at:national="1" at:sequenceNumber="002" at:SID="-17893">
            </measureCondition>
            <measureFootnoteAssociation at:footnoteId="030" at:footnoteType="02" at:national="1"/>
            <measureFootnoteAssociation at:footnoteId="018" at:footnoteType="02" at:national="1"/>
        </measure>
    </items>
</export>

Part of the output XML:

<?xml version="1.0" encoding="utf-8"?>
<at:export xsi:schemaLocation="http://www.arcticgroup.se/tariff/arctictariff/export Measure.xsd " xmlns="http://www.arcticgroup.se/tariff/arctictariff/export" xmlns:at="http://www.arcticgroup.se/tariff/arctictariff/export" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <at:id>98f8050f-97fb-4c73-bf89-2a332ccc0715</at:id>
  <at:exportType>MeasureObject</at:exportType>
  <at:parameters>
    <at:queryDateStart>2005-01-01</at:queryDateStart>
    <at:queryDateEnd>2020-01-01</at:queryDateEnd>
  </at:parameters>
  <items xmlns="">
    <at:measure at:geographicalAreaId="GCCG" at:goodsNomenclatureCode="1209220000" at:SIDGoodsNomenclature="32175" at:measureType="SEVAX" at:national="1" at:regulationId="1VAXTSKY" at:regulationRoleType="1" at:SID="-31825" at:SIDGeographicalArea="-10006" at:dateStart="2016-08-21" at:stoppedFlag="0" at:changeType="U">
      <at:measureCondition at:actionCode="01" at:certificateCode="006" at:certificateType="9" at:conditionCodeId="B" at:expression="[0.0,AMOUNT(0.0,&quot;SEK&quot;),TX]" at:national="1" at:sequenceNumber="001" at:SID="-17892"></at:measureCondition>
      <at:measureCondition at:actionCode="01" at:conditionCodeId="B" at:expression="$Base1 = ?KGM; $Rate = AMOUNT(0.09,&quot;SEK&quot;); $CalculatedResult = $Base1 * $Rate; $BaseLow = AMOUNT(1200.0,&quot;SEK&quot;); AMAX([$BaseLow, 1.0,VA],[$CalculatedResult, 1.0,VA])" at:national="1" at:sequenceNumber="002" at:SID="-17893"></at:measureCondition>
      <at:measureFootnoteAssociation at:footnoteId="030" at:footnoteType="02" at:national="1" />
      <at:measureFootnoteAssociation at:footnoteId="018" at:footnoteType="02" at:national="1" />
    </at:measure>
  </items>
</at:export>
1

There are 1 answers

0
jdweng On

I usually do following to avoid namespace issues :

            string xml =
                "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
                "<export xsi:schemaLocation=\"http://www.arcticgroup.se/tariff/arctictariff/export Measure.xsd \"" +
                " xmlns=\"http://www.arcticgroup.se/tariff/arctictariff/export\"" +
                " xmlns:at=\"http://www.arcticgroup.se/tariff/arctictariff/export\"" +
                " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
                "</export>";

            XDocument newRequest = XDocument.Parse(xml);