2

I have static method CreateXml where i create xml document. i get value of node and then i want add this values to list. my first append work fine, but second append dont work. How it fix and maybe you know how it code clear? screen here

private static void CreateXml(DataReader reader)
{
    var tempServerDir = Path.Combine(tempExportDirectory, Text.GetRandomString() + ".xml");
    XmlDocument xml=new XmlDocument();
    /// <summary>
    ///Create Head xml element
    /// </summary>
    xml.CreateElement("Head");
    var uniqueIdentifier = xml.CreateElement("Unique_identifier");
    var documentDate = xml.CreateElement("Document_date");
    var documentNumber = xml.CreateElement("Document_number");
    var documentName = xml.CreateElement("Document_name");
    var vehicleId = xml.CreateElement("Vehicle_id");
    var company = xml.CreateAttribute("Company");
    var driverId = xml.CreateAttribute("Driver_id");
    var contractId=xml.CreateAttribute("Contract_id");

    /// <summary>
    ///get value of node
    /// </summary>
    uniqueIdentifier.InnerText = Text.Convert(reader.GetFieldValue<int>("uniqueIdentifier"));
    documentDate.InnerText = Text.Convert(reader.GetFieldValue<DateTime>("documentDate"));
    documentNumber.InnerText = reader.GetFieldValue<string>("documentNumber");
    documentName.InnerText = "NDMTtest";
    //documentName.InnerText = reader.GetFieldValue<string>("documentName");
    vehicleId.InnerText = Text.Convert(reader.GetFieldValue<int>("vehicleId"));
    company.InnerText = Text.Convert(reader.GetFieldValue<int>("company"));
    driverId.InnerText = Text.Convert(reader.GetFieldValue<int>("driverId"));
    contractId.InnerText = reader.GetFieldValue<string>("contractId");

    /// <summary>
    ///add values to list
    /// </summary>
    xml.AppendChild(uniqueIdentifier);
    //xml.AppendChild(documentDate);
    //xml.AppendChild(documentNumber);
    //xml.AppendChild(documentName);
    //xml.AppendChild(vehicleId);
    //xml.AppendChild(driverId);
    //xml.AppendChild(company);
    //xml.AppendChild(contractId);
    /// <summary>
    ///Create Order xml element
    /// </summary>
    xml.CreateElement("Order");
    var testOrder = xml.CreateAttribute("NPP");
    testOrder.InnerText = "TestString";
    xml.AppendChild(testOrder);
    InfoManager.MessageBox("tempServerDir4:{0}", tempServerDir);
    /// <summary>
    /// Save and send file to client from temp directory
    /// </summary>
    //xml.Save(Console.Out);
    xml.Save(tempServerDir);
    //FileManager.SendFile(tempServerDir);
    InfoManager.MessageBox(FileManager.SendFile(tempServerDir));

}
2
  • 1
    The // means that it is a comment, not code. Comments are just there for programmers to read, but they won't be executed. Remove the // and it might work. The colors of the text are there to help. Commented Feb 6, 2019 at 13:40
  • i coment it because this code dont work after second xml.AppendChild Commented Feb 6, 2019 at 13:43

2 Answers 2

3

xml is an XmlDocument, which represents an XML document. An XML document can only have a single root element, so adding more than one isn't possible. This is most probably why your commented code 'doesn't work'.

I suspect you wanted to add these elements to the Head element you create at the beginning. So a simplified version of your code:

var xml = new XmlDocument();

var head = xml.CreateElement("Head");
var uniqueIdentifier = xml.CreateElement("Unique_identifier");
var documentDate = xml.CreateElement("Document_date");
var documentNumber = xml.CreateElement("Document_number");

uniqueIdentifier.InnerText = "1";
documentDate.InnerText = "2019-01-01";
documentNumber.InnerText = "2";

xml.AppendChild(head);
head.AppendChild(uniqueIdentifier);
head.AppendChild(documentDate);
head.AppendChild(documentNumber);

See this fiddle for a working demo.

However, I'd strongly suggest you consider using the newer LINQ to XML APIs instead, as they're much easier to work with. The same code above could be written as below:

var doc = new XDocument(
    new XElement("Head",
        new XElement("Unique_identifier", "1"),
        new XElement("Document_date", "2019-01-01"),
        new XElement("Document_number", "2")
    )
);

See this fiddle for another demo.

Both result in XML like below:

<Head>
  <Unique_identifier>1</Unique_identifier>
  <Document_date>2019-01-01</Document_date>
  <Document_number>2</Document_number>
</Head>
Sign up to request clarification or add additional context in comments.

1 Comment

my system dont see System.Xml.Lonq))))THX
2

The problem is that you are using the library in a wrong way, one possible way of doing this is to fill DocumentElement by using the Load method then append children to it, following code should work:

        xml.Load(new StringReader("<root></root>"));

        /// <summary>
        ///add values to list
        /// </summary>
        xml.DocumentElement.AppendChild(uniqueIdentifier);
        xml.DocumentElement.AppendChild(documentDate);
        xml.DocumentElement.AppendChild(documentNumber);
        xml.DocumentElement.AppendChild(documentName);
        xml.DocumentElement.AppendChild(vehicleId);

        //add attributes 
        xml.DocumentElement.Attributes.Append(driverId);
        xml.DocumentElement.Attributes.Append(company);
        xml.DocumentElement.Attributes.Append(contractId);

        xml.Save(Console.Out);

//output
//<root Driver_id="driverId" Company="company" Contract_id="contractId">
  //<Unique_identifier>uniqueIdentifier</Unique_identifier>
  //<Document_date>documentDate</Document_date>
  //<Document_number>documentNumber</Document_number>
  //<Document_name>NDMTtest</Document_name>
  //<Vehicle_id>vehicleId</Vehicle_id>
//</root>

Also note that attributes have different way of adding them check updated code.

Check MSDN documetnation.

2 Comments

Although this method creates the new object in the context of the document, it does not automatically add the new object to the document tree. To add the new object, you must explicitly call one of the node insert methods - this?
@Nastya also see attributes way of adding

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.