XElement生成以“<at:”开头的名称

时间:2016-12-29 13:23:05

标签: c# xml xelement

=“”

分割大型XML文件时遇到问题。当我生成要插入新的小文件的新元素(从原始文件复制)时,它们都以<at:

开头

例如,我的根元素如下所示:<at:export

而不是:<export

它们与Unix LF具有相同的编码(UTF-8)。

任何知道原因的人吗?

修改: 我的分割函数中的代码

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;
    }

源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>

输出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 个答案:

答案 0 :(得分:0)

我通常会遵循以避免名称空间问题:

            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);