Jump to content

Ler ficheiro XML - Tags específicas


AlexG

Recommended Posts

Boas,

Tenho o seguinte ficheiro XML(gerado de um ficheiro EDI) fornecido por uma empresa e tenho de recolher a informação para guardar numa base dados, no entanto não estou a conseguir ler este XML. Gostaria de saber se me podem ajudar a ler Tags específicas, por exemplo:

Quero a informação do nó <BGM> , Tag "messageNumber"= "", numa textbox...

Obrigado.

<?xml version="1.0" encoding="utf-8"?>
<D07ADESADV  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.default.com/D07A/desadv">
  <DESADV>
    <UNH referenceNumber="1" typeIdentifier="DESADV" versionNumber="D" releaseNumber="07A" controllingAgency="UN" associationAssignedCode="GAVF13" />
    <BGM messageName="351" codeListResponsibleAgency="9" messageNumber="7539351" />
    <DTM dateTimePeriodQualifier="137" dateTimePeriod="20210823" dateTimePeriodFormatQualifier="102" />
    <GRP1>
      <RFF referenceQualifier="ON" referenceNumber="5500265331" />
    </GRP1>
    <GRP1>
      <RFF referenceQualifier="ON" referenceNumber="5500265332" />
    </GRP1>
    <GRP2>
      <NAD partyQualifier="BY" partyIDIdentification="6104" codeListQualifier="0" codeListResponsibleAgency="91" nameAndAddress="" partyName="BUYER" streetName="" cityName="" countrySubEntityID="" postCodeID="" countryCoded="CZ" />
    </GRP2>
    <GRP2>
      <NAD partyQualifier="SF" partyIDIdentification="0000066114" codeListQualifier="0" codeListResponsibleAgency="92" nameAndAddress="*SCHERDEL" partyName="" streetName="" cityName="" countrySubEntityID="CZ" />
    </GRP2>
    <GRP2>
      <NAD partyQualifier="ST" partyIDIdentification="6104" codeListQualifier="0" codeListResponsibleAgency="92" nameAndAddress="" partyName="SHIP-TO-CUSTOMER" streetName="" cityName="" countrySubEntityID="" postCodeID="CZ" />
    </GRP2>
    <GRP2>
      <NAD partyQualifier="FW" partyIDIdentification="BOR_EIGEN" codeListQualifier="0" codeListResponsibleAgency="92" nameAndAddress="" />
    </GRP2>
    <GRP2>
      <NAD partyQualifier="SE" partyIDIdentification="0000066114" codeListQualifier="0" codeListResponsibleAgency="92" nameAndAddress="" partyName="SCHERDEL" streetName="" cityName="" countrySubEntityID="" postCodeID="CZ" />
    </GRP2>
    <GRP5>
      <TOD todFunction="6" todCoded="FCA" />
    </GRP5>
    <GRP10>
      <CPS hierarchicalIdNumber="1" hierachticalParentID="0" packagingLevelCoded="3">
        <GRP11>
          <PAC numberOfPackages="1" packagingLevel="0" packagingRelatedInformation="0" packagingTermsAndConditions="0" packageTypeID="35" codeListQualifier="0" codeListResponsibleAgency="0" itemDescriptionType="0" typeOfPackages="AAA" itemNumberType="0" rtnPackFreightPmtResponsibility="0" rtnPackLoadContents="0">
            <GRP13>
              <PCI markingInstructionsCoded="17" shippingMarks="" containerPackageStatus="0">
                <GRP14>
                  <GIN identityNumberQualifier="ML" identityNumber="232008397" />
                </GRP14>
                <GRP14>
                  <GIN identityNumberQualifier="AW" identityNumber="232008398" />
                </GRP14>
                <GRP14>
                  <GIN identityNumberQualifier="AW" identityNumber="232008403" />
                </GRP14>
                <GRP14>
                  <GIN identityNumberQualifier="AW" identityNumber="232008408" />
                </GRP14>
                <GRP14>
                  <GIN identityNumberQualifier="AW" identityNumber="232008413" />
                </GRP14>
                <GRP14>
                  <GIN identityNumberQualifier="AW" identityNumber="232008418" />
                </GRP14>
                <GRP14>
                  <GIN identityNumberQualifier="AW" identityNumber="232008423" />
                </GRP14>
                <GRP14>
                  <GIN identityNumberQualifier="AW" identityNumber="232008428" />
                </GRP14>
              </PCI>
            </GRP13>
          </PAC>
        </GRP11>
        <GRP11>
          <PAC numberOfPackages="1" packagingLevel="0" packagingRelatedInformation="0" packagingTermsAndConditions="0" packageTypeID="37" codeListQualifier="0" codeListResponsibleAgency="0" itemDescriptionType="0" typeOfPackages="A1208ESD" itemNumberType="0" rtnPackFreightPmtResponsibility="0" rtnPackLoadContents="0" />
        </GRP11>
      </CPS>
    </GRP10>
    <GRP10>
      <CPS hierarchicalIdNumber="2" hierachticalParentID="0" packagingLevelCoded="1">
        <GRP11>
          <PAC numberOfPackages="32" packagingLevel="0" packagingRelatedInformation="0" packagingTermsAndConditions="0" packageTypeID="35" codeListQualifier="0" codeListResponsibleAgency="0" itemDescriptionType="0" typeOfPackages="KLT RAKO 4030 ESD" itemNumberType="0" rtnPackFreightPmtResponsibility="0" rtnPackLoadContents="0">
            <GRP13>
              <PCI markingInstructionsCoded="17" shippingMarks="" containerPackageStatus="0">
                <GRP14>
                  <GIN identityNumberQualifier="ML" identityNumber="232008398" />
                </GRP14>
                <GRP14>
                  <GIN identityNumberQualifier="ML" identityNumber="232008403" />
                </GRP14>
                <GRP14>
                  <GIN identityNumberQualifier="ML" identityNumber="232008408" />
                </GRP14>
                <GRP14>
                  <GIN identityNumberQualifier="ML" identityNumber="232008413" />
                </GRP14>
                <GRP14>
                  <GIN identityNumberQualifier="ML" identityNumber="232008418" />
                </GRP14>
                <GRP14>
                  <GIN identityNumberQualifier="ML" identityNumber="232008423" />
                </GRP14>
                <GRP14>
                  <GIN identityNumberQualifier="ML" identityNumber="232008428" />
                </GRP14>
              </PCI>
            </GRP13>
          </PAC>
        </GRP11>
        <GRP11>
          <PAC numberOfPackages="32" packagingLevel="0" packagingRelatedInformation="0" packagingTermsAndConditions="0" packageTypeID="37" codeListQualifier="0" codeListResponsibleAgency="0" itemDescriptionType="0" typeOfPackages="D RAKO 4030 ESD" itemNumberType="0" rtnPackFreightPmtResponsibility="0" rtnPackLoadContents="0" />
        </GRP11>
        <GRP11>
          <PAC numberOfPackages="64" packagingLevel="0" packagingRelatedInformation="0" packagingTermsAndConditions="0" packageTypeID="37" codeListQualifier="0" codeListResponsibleAgency="0" itemDescriptionType="0" typeOfPackages="BLISTER TT-06583" itemNumberType="0" rtnPackFreightPmtResponsibility="0" rtnPackLoadContents="0" />
        </GRP11>
        <GRP11>
          <PAC numberOfPackages="32" packagingLevel="0" packagingRelatedInformation="0" packagingTermsAndConditions="0" packageTypeID="37" codeListQualifier="0" codeListResponsibleAgency="0" itemDescriptionType="0" typeOfPackages="PE-BEUTEL ANTISTA" itemNumberType="0" rtnPackFreightPmtResponsibility="0" rtnPackLoadContents="0" />
        </GRP11>
        <GRP15>
          <LIN lineItemNumber="0" actionRequest="0" itemNumber="602-23014-03" itemNumberType="IN" codeListQualifier="0" codeListResponsibleAgency="0" configurationCoded="0">
            <IMD itemDescriptionType="" itemDescription="ZUSAMMENBAUTEIL" />
            <QTY qtyQualifier="12" quantity="384" measureUnitQualifier="PCE" />
            <GRP16>
              <RFF referenceQualifier="ON" referenceNumber="5500265331" />
            </GRP16>
          </LIN>
        </GRP15>
      </CPS>
    </GRP10>
    <GRP10>
      <CPS hierarchicalIdNumber="3" hierachticalParentID="0" packagingLevelCoded="3">
        <GRP11>
          <PAC numberOfPackages="1" packagingLevel="0" packagingRelatedInformation="0" packagingTermsAndConditions="0" packageTypeID="35" codeListQualifier="0" codeListResponsibleAgency="0" itemDescriptionType="0" typeOfPackages="AAA" itemNumberType="0" rtnPackFreightPmtResponsibility="0" rtnPackLoadContents="0" />
        </GRP11>
        <GRP11>
          <PAC numberOfPackages="1" packagingLevel="0" packagingRelatedInformation="0" packagingTermsAndConditions="0" packageTypeID="37" codeListQualifier="0" codeListResponsibleAgency="0" itemDescriptionType="0" typeOfPackages="A1208ESD" itemNumberType="0" rtnPackFreightPmtResponsibility="0" rtnPackLoadContents="0" />
        </GRP11>
      </CPS>
    </GRP10>
    <GRP10>
      <CPS hierarchicalIdNumber="4" hierachticalParentID="0" packagingLevelCoded="1">
        <GRP11>
          <PAC numberOfPackages="32" packagingLevel="0" packagingRelatedInformation="0" packagingTermsAndConditions="0" packageTypeID="35" codeListQualifier="0" codeListResponsibleAgency="0" itemDescriptionType="0" typeOfPackages="KLT RAKO 4030 ESD" itemNumberType="0" rtnPackFreightPmtResponsibility="0" rtnPackLoadContents="0" />
        </GRP11>
        <GRP11>
          <PAC numberOfPackages="32" packagingLevel="0" packagingRelatedInformation="0" packagingTermsAndConditions="0" packageTypeID="37" codeListQualifier="0" codeListResponsibleAgency="0" itemDescriptionType="0" typeOfPackages="D RAKO 4030 ESD" itemNumberType="0" rtnPackFreightPmtResponsibility="0" rtnPackLoadContents="0" />
        </GRP11>
        <GRP11>
          <PAC numberOfPackages="64" packagingLevel="0" packagingRelatedInformation="0" packagingTermsAndConditions="0" packageTypeID="37" codeListQualifier="0" codeListResponsibleAgency="0" itemDescriptionType="0" typeOfPackages="BLISTER TT-06584" itemNumberType="0" rtnPackFreightPmtResponsibility="0" rtnPackLoadContents="0" />
        </GRP11>
        <GRP11>
          <PAC numberOfPackages="32" packagingLevel="0" packagingRelatedInformation="0" packagingTermsAndConditions="0" packageTypeID="37" codeListQualifier="0" codeListResponsibleAgency="0" itemDescriptionType="0" typeOfPackages="PE-BEUTEL ANTISTA" itemNumberType="0" rtnPackFreightPmtResponsibility="0" rtnPackLoadContents="0" />
        </GRP11>
        <GRP15>
          <LIN lineItemNumber="0" actionRequest="0" itemNumber="602-23015-03" itemNumberType="IN" codeListQualifier="0" codeListResponsibleAgency="0" configurationCoded="0">
            <IMD itemDescriptionType="" itemDescription="ZUSAMMENBAUTEIL" />
            <QTY qtyQualifier="12" quantity="512" measureUnitQualifier="PCE" />
            <GRP16>
              <RFF referenceQualifier="ON" referenceNumber="5500265332" />
            </GRP16>
          </LIN>
        </GRP15>
      </CPS>
    </GRP10>
    <UNT numberOfSegments="95" messageReferenceNumber="1" />
  </DESADV>
</D07ADESADV>
Link to comment
Share on other sites

Podes sempre gerar as classes (o visual Studio faz isso automaticamente com a opção Paste Special->Paste XML as Classes) e depois des-serializar o xml em objectos

public T ConvertFromXml<T>(ref string str) where T : class
{
    XmlSerializer serializer;
    try
    {
        serializer = new XmlSerializer(typeof(T));
    }
    catch (Exception ex)
    {
    }

    StringReader reader = new StringReader(str);
    T c = serializer.Deserialize(reader) as T;

    return c;
}

Depois é só navegar pelos objectos até chegares à informação que pretendes.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.