I'm using the following code to write an XML file to disk. If I change the values for each field and re-run the code, the values saved will simply be replaced.
I've looked around here but I see no way to automatically append the new values to the end of the file as a new element and not simply replace everything.
XNamespace empNM = "urn:lst-emp:emp";
XDocument xDoc = new XDocument(
new XDeclaration("1.0", "UTF-16", null),
new XElement(empNM + "Employees",
new XElement("Employee",
new XComment("Only 3 elements for demo purposes"),
new XElement("EmpId", "5"),
new XElement("Name", "Kimmy"),
new XElement("Sex", "Female")
)));
StringWriter sw = new StringWriter();
XmlWriter xWrite = XmlWriter.Create(sw);
xDoc.Save(xWrite);
xWrite.Close();
// Save to Disk
xDoc.Save("C:\\tempFolder\\test.xml");
Console.WriteLine("Saved");
Also, could someone please explain what "urn:lst-emp:emp"; in the first line does.
void Main()
{
XNamespace empNM = "urn:lst-emp:emp";
XDocument xDoc ;
string path="C:\\tempFolder\\test.xml";
if(!File.Exists(path))
{
xDoc = new XDocument(
new XDeclaration("1.0", "UTF-16", null),
new XElement(empNM + "Employees")
);
}
else
{
xDoc=XDocument.Load(path);
}
var element=new XElement("Employee",
new XComment("Only 3 elements for demo purposes"),
new XElement("EmpId", "5"),
new XElement("Name", "Kimmy"),
new XElement("Sex", "Female"));
xDoc.Element(empNM+"Employees").Add(element);
// Save to Disk
xDoc.Save(path);
Console.WriteLine("Saved");
}
Here is the Xml generated:
<?xml version="1.0" encoding="utf-16"?>
<Employees xmlns="urn:lst-emp:emp">
<Employee xmlns="">
<!--Only 3 elements for demo purposes-->
<EmpId>5</EmpId>
<Name>Kimmy</Name>
<Sex>Female</Sex>
</Employee>
<Employee xmlns="">
<!--Only 3 elements for demo purposes-->
<EmpId>5</EmpId>
<Name>Kimmy</Name>
<Sex>Female</Sex>
</Employee>
</Employees>
Related
My XML file
<?xml version="1.0" encoding="utf-8"?>
<Root xmlns="http://a01_data_navin"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://a01_data_navin event.xsd">
<Event>
<eventid>1</eventid>
<Photo>
<filepath>files\images\memory rep02.png</filepath>
<location>
<lat>35.496456056584158</lat>
<lon>-99.228515625</lon>
</location>
<datetimestamp>2020-03-29T00:00:00</datetimestamp>
</Photo>
</Event>
<Event>
<eventid>2</eventid>
<Photo>
<filepath>files\images\poop.jpeg</filepath>
<location>
<lat>36.137874718407268</lat>
<lon>-89.6044921875</lon>
</location>
<datetimestamp>2020-03-29T00:00:00</datetimestamp>
</Photo>
</Event>
</Root>
When i use XDocument in C#
XDocument xml = XDocument.Load(_xmlFilePath);
// create xml structure
var photo_XML = tempPhotoList.ToArray();
xml.Element("Root")?.Add(
new XElement("Event",
from photo in photo_XML
select new XElement("eventid", photo.EventId),
from photo1 in photo_XML
select new XElement("Photo",
new XElement("filepath", photo1.FileNameForPath),
new XElement("location",
new XElement("lat", photo1.GetLatitude()),
new XElement("lon", photo1.GetLongitude())),
new XElement("datetimestamp", photo1.DateTimeStamp)
))
);
When i run the above code i cant seem to enter into the xml file and iterate the tree. I would have to add a name space like this:
XDocument xml = XDocument.Load(_xmlFilePath);
// create xml structure
var photo_XML = tempPhotoList.ToArray();
xml.Element(NameSpace+"Root")?.Add(
new XElement("Event",
from photo in photo_XML
select new XElement("eventid", photo.EventId),
from photo1 in photo_XML
select new XElement("Photo",
new XElement("filepath", photo1.FileNameForPath),
new XElement("location",
new XElement("lat", photo1.GetLatitude()),
new XElement("lon", photo1.GetLongitude())),
new XElement("datetimestamp", photo1.DateTimeStamp)
))
);
With adding the Element(NameSpace+"Root"); im able to traverse through my xml file and add a new event but i end up with this..
<Event xmlns="">
<eventid>3</eventid>
<Photo>
<filepath>files\images\poop.jpeg</filepath>
<location>
<lat>17.140790393316649</lat>
<lon>1.7578125</lon>
</location>
<datetimestamp>2020-03-29T00:00:00</datetimestamp>
</Photo>
</Event>
I need some help on how to add or update a new event into an existing xml file which has namespace to deal with; using XDocument in C# ? It seems that using XDocument gets my task done.
Im stuck guys.. please help..
Try following :
XDocument xml = XDocument.Load(_xmlFilePath);
XElement root = xml.Root;
XNamespace ns = root.GetDefaultNamespace();
root.Add(new XElement(ns + "Event",
Hey everyone so this is what i did to resolve the issue in hand..
*I added namespace to every XElement instead of only at the root.. Sorry rookie mistake from before.. :P
XElement root = xml.Root;
XNamespace ns = root.GetDefaultNamespace();
root.Add(
new XElement(ns+"Event",
from photo in photo_XML
select new XElement(ns +"eventid", photo.EventId),
from photo1 in photo_XML
select new XElement(ns +"Photo",
new XElement(ns +"filepath", photo1.FileNameForPath),
new XElement(ns +"location",
new XElement(ns +"lat", photo1.GetLatitude()),
new XElement(ns +"lon", photo1.GetLongitude())),
new XElement(ns +"datetimestamp", photo1.DateTimeStamp)
))
);
I'm attempting to write a small XML file using c# Linq XDocument.
The final xml file should look like this:
<?xml version="1.0" encoding="utf-8"?>
<Root>
<Asset InternalID="SOMEID" LastSaveDate="2016-10-28" LastSaveTime="01:01:33:00" AssetType="New">
<type_metadata>
<FIELD name="filename">SOMEID.MOV</FIELD>
<FIELD name="duration">00:00:00:10</FIELD>
</type_metadata>
</Asset>
</Root>
Here is my code:
XDocument doc = new XDocument(new XDeclaration("1.0", "UTF-8", null));
doc.Add(new XElement("Root"));
doc.Element("Root").Add(new XElement("Asset"));
doc.Element("Root").Element("Asset").Add(new XAttribute("InternalID", a.InternalID));
doc.Element("Root").Element("Asset").Add(new XAttribute("LastSaveDate", a.lastSaveDate));
doc.Element("Root").Element("Asset").Add(new XAttribute("LastSaveTime", a.lastSaveTime));
doc.Element("Root").Element("Asset").Add(new XAttribute("AssetType", a.AssetType));
doc.Element("Root").Element("Asset").Add(new XElement("type_metadata"));
doc.Element("Root").Element("Asset").Element("type_metadata").Add(new XElement("FIELD"));
doc.Element("Root").Element("Asset").Element("type_metadata").Element("FIELD").Add(new XAttribute("name","filename"));
doc.Element("Root").Element("Asset").Element("type_metadata").Element("FIELD").Value = a.filename;
doc.Element("Root").Element("Asset").Element("type_metadata").Add(new XElement("FIELD"));
doc.Element("Root").Element("Asset").Element("type_metadata").Element("FIELD").Add(new XAttribute("name", "duration"));
doc.Element("Root").Element("Asset").Element("type_metadata").Element("FIELD").Value = a.duration;
Everything works fine until I try to put in the second "FIELD" element.
What is the proper way to do this? I have done some research, but I cant find a simple answer that is directly relevant to what I'm trying to accomplish.
That's because when you're trying to add second element you're using:
doc.Element("Root").Element("Asset").Element("type_metadata").Element("FIELD")
It will return first element matching the name, which is this case is the previously added "FIELD" element, which already has "name" attribute.
I'd suggest you create the element itself before attaching it to the document. This way you won't have to search for the element over and over again:
XDocument doc = new XDocument(new XDeclaration("1.0", "UTF-8", null));
var root = new XElement("Root");
var asset = new XElement("Asset");
asset.Add(new XAttribute("InternalID", a.InternalID));
asset.Add(new XAttribute("LastSaveDate", a.lastSaveDate));
asset.Add(new XAttribute("LastSaveTime", a.lastSaveTime));
asset.Add(new XAttribute("AssetType", a.AssetType));
var type_metadata = new XElement("type_metadata");
var field = new XElement("FIELD");
field.Add(new XAttribute("name","filename"));
field.Value = a.filename;
type_metadata.Add(field);
var field2 = new XElement("FIELD");
field2.Add(new XAttribute("name","duration"));
field2.Value = a.duration;
type_metadata.Add(field2);
asset.Add(type_metadata);
root.Add(asset);
doc.Add(root);
Also, you can create the entire document in a single statement:
XDocument doc = new XDocument(
new XDeclaration("1.0", "UTF-8", null),
new XElement("Root",
new XElement("Asset",
new XAttribute("InternalID", a.InternalID),
new XAttribute("LastSaveDate", a.lastSaveDate),
new XAttribute("LastSaveTime", a.lastSaveTime),
new XAttribute("AssetType", a.AssetType),
new Element("type_metadata",
new XElement("FIELD",
new XAttribute("name", "filename"),
a.filename),
new XElement("FIELD",
new XAttribute("name", "duration"),
a.duration)))));
It would be easier if you prepare new FIELD element before adding it to the parent element :
var filename = new XElement("FIELD",
new XAttribute("name","filename"),
a.filename
);
var duration = new XElement("FIELD",
new XAttribute("name","duration"),
a.duration
);
doc.Element("Root").Element("Asset").Element("type_metadata").Add(field);
doc.Element("Root").Element("Asset").Element("type_metadata").Add(duration);
This can be achieved in different ways. I just followed your approach. Since you have multiple FIELD elements, slight modification required in your code. The following code will work as expected.
XDocument doc = new XDocument(new XDeclaration("1.0", "UTF-8", null));
doc.Add(new XElement("Root"));
doc.Element("Root").Add(new XElement("Asset"));
doc.Element("Root").Element("Asset").Add(new XAttribute("InternalID", "intID"));
doc.Element("Root").Element("Asset").Add(new XAttribute("LastSaveDate", "28.10.2016"));
doc.Element("Root").Element("Asset").Add(new XAttribute("LastSaveTime", "1.48PM"));
doc.Element("Root").Element("Asset").Add(new XAttribute("AssetType", "Laptop"));
doc.Element("Root").Element("Asset").Add(new XElement("type_metadata"));
doc.Element("Root").Element("Asset").Element("type_metadata").Add(new XElement("FIELD"));
doc.Element("Root").Element("Asset").Element("type_metadata").Element("FIELD").Add(new XAttribute("name", "filename"));
doc.Element("Root").Element("Asset").Element("type_metadata").Element("FIELD").Value = "a.txr";
doc.Element("Root").Element("Asset").Element("type_metadata").Add(new XElement("FIELD"));
doc.Element("Root").Element("Asset").Element("type_metadata").Elements().Last().Add(new XAttribute("name", "duration"));
doc.Element("Root").Element("Asset").Element("type_metadata").Elements().Last().Value = "00:12:98";
I'm writing to an XML file from which I'm going to retrieve data later.
Here's how I'm writing to the file.
XNamespace testNM = "urn:lst-emp:emp";
XDocument xDoc;
string path = "project_data.xml";
if (!File.Exists(path))
{
xDoc = new XDocument(
new XDeclaration("1.0", "UTF-16", null),
new XElement(testNM + "Test")
);
}
else
{
xDoc = XDocument.Load(path);
}
var element = new XElement("key",
new XAttribute("name", key),
new XElement("Type", type),
new XElement("Value", value));
xDoc.Element(testNM + "Test").Add(element);
// Save to Disk
xDoc.Save(path);
And this is what my XML file looks like after data is written to it.
<?xml version="1.0" encoding="utf-16"?>
<Test xmlns="urn:lst-emp:emp">
<key name="key2" xmlns="">
<Type>int</Type>
<Value>12312</Value>
</key>
<key name="key3" xmlns="">
<Type>String</Type>
<Value>asdfasd</Value>
</key>
</Test>
Now what would be the simplest way to get the name attribute value (key2 and key3 in this case) along with the Type and Value attribute values.
Load the document;
XDocument doc = XDocument.Load(#"doc.xml");
Loop the key nodes reading what you need;
foreach (var keyNode in doc.Root.Elements("key"))
{
var name = keyNode.Attribute("name");
var type = (string)keyNode.Element("Type"); // or .value to throw if there is no node
...
}
Building an XML XDocument to push to web service and the root element needs namespace values this is what the XML form should look like....
<shipment-feed xmlns="http://seller.marketplace.sears.com/oms/v5"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://seller.marketplace.sears.com/oms/v5 asn.xsd ">
<shipment>
<header>
<asn-number>00601780002</asn-number>
<po-number>0060180</po-number>
<po-date>2009-09-26</po-date>
</header>
<detail>
<tracking-number>UPS1XXX</tracking-number>
<ship-date>2001-01-01</ship-date>
<shipping-carrier>UPS</shipping-carrier>
<shipping-method>GROUND</shipping-method>
<package-detail>
<line-number>1</line-number>
<item-id>AB12345678912345456789123456789CD</item-id>
<quantity>1</quantity>
</package-detail>
</detail>
</shipment>
</shipment-feed>
This is the xml that I'm getting....
<?xml version="1.0" encoding="utf-8"?>
<shipment-feed xmlns="http://seller.marketplace.sears.com/oms/v5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsischemalocation="http://seller.marketplace.sears.com/oms/v5 asn.xsd">
<shipment xmlns="">
<header>
<asn-number>2824565201311</asn-number>
<po-number>2824565</po-number>
<po-date>2013-11-14</po-date>
</header>
<details>
<tracking-number>579040914892</tracking-number>
<ship-date>2013-11-14</ship-date>
<shipping-carrier>FEDEX</shipping-carrier>
<shipping-method>Ground</shipping-method>
<package-details>
<line-number>1</line-number>
<item-id>LTH7XB1MW-EA</item-id>
<quantity>3</quantity>
</package-details>
</details>
</shipment>
<shipment xmlns="">
<header>
<asn-number>2821596201311</asn-number>
<po-number>2821596</po-number>
<po-date>2013-11-13</po-date>
</header>
<details>
<tracking-number>9405515901119923380663</tracking-number>
<ship-date>2013-11-14</ship-date>
<shipping-carrier>USPS</shipping-carrier>
<shipping-method>Priority Mail</shipping-method>
<package-details>
<line-number>1</line-number>
<item-id>CWD93151-EA</item-id>
<quantity>6</quantity>
</package-details>
<package-details>
<line-number>2</line-number>
<item-id>CWD93901-EA</item-id>
<quantity>4</quantity>
</package-details>
</details>
</shipment>
</shipment-feed>
This is the C# code I created....
XNamespace ns1 = "http://seller.marketplace.sears.com/oms/v5";
XNamespace ns2 = "http://www.w3.org/2001/XMLSchema-instance";
XNamespace ns3 = "http://seller.marketplace.sears.com/oms/v5 asn.xsd";
XDocument doc = new XDocument();
XElement root = new XElement(ns1 + "shipment-feed",
new XAttribute("xmlns" , "http://seller.marketplace.sears.com/oms/v5"),
new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
new XAttribute("xsi" + "schemalocation", "http://seller.marketplace.sears.com/oms/v5 asn.xsd"));
doc.Add(root);
int x = 1;
foreach (SearsOrder s in SearsList)
{
XElement shipment = new XElement("shipment",
new XElement("header",
new XElement("asn-number", s.asnnumber),
new XElement("po-number", s.ponumber),
new XElement("po-date", s.podate)),
new XElement("details",
new XElement("tracking-number", s.trackingnum),
new XElement("ship-date", s.shipdate),
new XElement("shipping-carrier", s.carrier),
new XElement("shipping-method", s.method),
s.orderitems.Select(i => new XElement("package-details",
new XElement("line-number", x++),
new XElement("item-id", i.itemid),
new XElement("quantity", i.quantity)))
));
doc.Root.Add(shipment);
x = 1;
}
The fist problem is the first child node I'm not seeing where that is coming from because that node is not even declared until the foreach loop. I was under the impression that I was only adding attributes to the root element.
and the other problem is removing the xml declaration
This sort of the thing is the problem:
new XElement("shipment", ...)
You want the shipment elements to be in the "http://seller.marketplace.sears.com/oms/v5" namespace - so you need to make that explicit. That won't show up in the resulting XML directly, because they'll inherit that as the default namespace specified in the root. Basically, wherever you're creating an element, you probably want to use ns1. So:
new XElement(ns1 + "shipment", new XElement(ns1 + "header", ...), ...)
To understand more about why this is required, you should read up on namespace defaulting in the XML Namespaces specification.
and the other problem is removing the xml declaration
So add an XDeclaration to the XDocument:
XDocument doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"))
I want to write a XML file as below:
<?xml version="1.0" encoding="UTF-8"?>
<books xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<License licenseId="" licensePath="" />
Some piece of my code attached here
// Create a new file in D:\\ and set the encoding to UTF-8
XmlTextWriter textWriter = new XmlTextWriter("D:\\books.xml", System.Text.Encoding.UTF8);
// Format automatically
textWriter.Formatting = Formatting.Indented;
// Opens the document
textWriter.WriteStartDocument();
// Write the namespace declaration.
textWriter.WriteStartElement("books", null);
// Write the genre attribute.
textWriter.WriteAttributeString("xmlns", "xsd", null, "http://www.w3.org/2001/XMLSchema");
textWriter.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
And now I need to write the License Line below in C#
<License licenseId="" licensePath="" />
But I don't know how to move on for I found the Line ended with the forward slash / .Thank you.
I have 2 questions about the way you're doing this:
1) Do you have to use a text writer? If you have access to c# 3.0 then you can use the following:
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XAttribute(XNamespace.Xmlns + "xsd", "http://www.w3.org/2001/XMLSchema"),
new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
new XElement("Equipment",
new XElement("License",
new XAttribute("licenseId", ""),
new XAttribute("licensePath", "")
)
)
);
2) Do you have to declare the two namespaces? It seems to me like you won't use them:
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XElement("Equipment",
new XElement("License",
new XAttribute("licenseId", ""),
new XAttribute("licensePath", "")
)
)
);
If you're intending to write multiple License elements to the document, and you have them in an Array, List or some other IEnumerable, you can use something similar to the code below to spit them all out:
IEnumerable<LicenceObjects> licenses = //some code to make them;
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XElement("Equipment",
licenses.Select(l =>
new XElement("License",
new XAttribute("licenseId", l.licenseId),
new XAttribute("licensePath", l.licensePath)
)
)
)
);
string xmlDocumentString = doc.ToString();
Of course, if you don't have .NET 3.0, then this is useless to you :(
Calling the WriteEndElement method will automatically take care of adding the forwards slash.
Why don't you just proceed as you started?
textWriter.WriteStartElement("Licence");
textWriter.WriteAttributeString("LicenseId", "");
textWriter.WriteAttributeString("LicensePath", "");
// Other stuff
textWriter.WriteEndDocument();
textWriter.Close();