Frage

I'm trying to best understand how to implement the following XML as an XDocument, but I'm rather new to the XDocument stuff and I'm running into a conceptual problem of how to get around the multi-attribute in an Element with a value nested inside another Element.

The below is a sample of the XML -- any help would be appreciated

<LVNPImport xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <InterfaceIdentifier>835</InterfaceIdentifier>
    <FolderPaths>
        <Folder fromDate="" toDate="" contactName="APerson" email="AnEmail">Remittance Advice</Folder>
        <Folder>%FACILITY%</Folder>
        <Folder>%PAYORID%</Folder>
        <Folder>%REMITDATE YYYY%</Folder>
        <Folder>%REMITDATE MMMM YYYY%</Folder>
    </FolderPaths>
    <DocumentType>RA</DocumentType>
    <DocumentDescription>%REMITDATE MM-DD-YY%</DocumentDescription>
    <TotalFiles>1</TotalFiles>
</LVNPImport>

EDIT

The above is a template -- I'm not reading in, I'm writing out so I need to create the above as an XDoc.

I'm just getting into the XDocument stuff and what I have is pretty much what you can find with most examples on Stack.

War es hilfreich?

Lösung

You can pass multiple attributes inside the new XElement overload, as the objects content:

XElement folderPath = new XElement("FolderPaths");
folderPath.Add(
    new XElement(
        "Folder", 
        new XAttribute("fromDate", String.Empty), 
        new XAttribute("toDate", String.Empty), 
        new XAttribute("contactName", "APerson"), 
        new XAttribute("email", "AnEmail"), 
        "Remittance Advice"
    )
);

Andere Tipps

For anyone looking at this, between the help I've gotten on this answer from @ReinderWit as well as the answer from another question (see the other comments in the question) I was able to figure out how to build up the XDocument.

The code follows:

            _folderviewContents =
            new XDocument(
                new XElement("InterfaceIdentifier", "835"),
                //Start of FolderPaths 
                new XElement("FolderPaths",
                    new XElement("Folder",
                        new XAttribute("fromDate", String.Empty),
                        //attributes for Folder w/ lots of attributes
                        new XAttribute("toDate", String.Empty),
                        new XAttribute("contactName", "APerson"),
                        new XAttribute("email", "AnEmail"),
                        //value for that long Folder w/ lots of attributes
                        "Remittance Advice"),
            //Facility
                    new XElement("Folder", String.Empty),
            //PayorID
                    new XElement("Folder", String.Empty),
            //RemitDate Year
                    new XElement("Folder", String.Empty),
            //RemitDate Month/Year
                    new XElement("Folder", String.Empty)),
            new XElement("DocumentType", "RA"),
            new XElement("DocumentDescription",String.Empty),
            new XElement("TotalFiles", "1"));

I still need to add the XML version and the Namespace, but those seem simple enough to do. Hopefully this helps others in the future with building XDocs that are a bit weird and not-so-straightforward.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top