Adding XElement to XML file using Linq to XML - windows

Using Linq to XML, I am trying to add an XElement to an existing XML file.
It has to be done in the Windows Phone .NET framework.
Currently my XML file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<Kids>
<Child>
<Name>Kid1</Name>
<FirstName>hisname</FirstName>
</Child>
</Kids>
and my code looks like this:
using (IsolatedStorageFileStream stream =
new IsolatedStorageFileStream("YourKids.xml", fileMode, store))
{
XDocument x = XDocument.Load(stream);
XElement t = new XElement("Child",
new XElement("Name", pName),
new XElement("FirstName", pFirstname));
t.Add(from el in x.Elements()
where el == el.Descendants("Child").Last()
select el);
x.Save(stream);
}
this doesn't do what I want to achieve. I want to add a new "Child" element to the the exisiting XML file like this :
<?xml version="1.0" encoding="utf-8"?>
<Kids>
<Child>
<Name>Kid1</Name>
<FirstName>hisname</FirstName>
</Child>
<Child>
<Name>kid2</Name>
<FirstName>SomeName</FirstName>
</Child>
</Kids>
Could use some help because I am stuck ;-)
After the tips from GSerjo, my code looks like this now:
try
{
if (store.FileExists("YourKids.xml"))
{
using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("YourKids.xml",FileMode.Open, store))
{
var x = XElement.Load(stream);
var t = new XElement("Child",
new XElement("Name", pName),
new XElement("FirstName", pFirstname)
);
x.Add(t);
x.Save(stream);
stream.Close();
return;
}
}
else
{
using (IsolatedStorageFileStream CreateIsf = new IsolatedStorageFileStream("YourKids.xml",FileMode.Create,store))
{
var xDoc = new XElement("Kids",
new XElement("Child",
new XElement("Name", pName),
new XElement("FirstName", pFirstname)
)
);
xDoc.Save(CreateIsf);
CreateIsf.Close();
return;
}
}
}
catch (Exception ex)
{
message = ex.Message;
}
Which gives me an xml file like this:
<?xml version="1.0" encoding="utf-8"?>
<Kids>
<Child>
<Name>test</Name>
<FirstName>test</FirstName>
</Child>
</Kids><?xml version="1.0" encoding="utf-8"?>
<Kids>
<Child>
<Name>test</Name>
<FirstName>test</FirstName>
</Child>
<Child>
<Name>testing</Name>
<FirstName>testing</FirstName>
</Child>
</Kids>
Which is still not correct, any ideas anyone ?

Initial xml file
<?xml version="1.0" encoding="utf-8"?>
<Kids>
<Child>
<Name>Kid1</Name>
<FirstName>hisname</FirstName>
</Child>
</Kids>
Following code add one new child to existing xml
[Test]
public void Test()
{
string filPath = #"YourKids.xml";
var root = XElement.Load(filPath);
var newChild = new XElement("Child",
new XElement("Name", "NewName"),
new XElement("FirstName", "NewFirstName"));
root.Add(newChild);
root.Save(filPath);
}
Result xml file
<?xml version="1.0" encoding="utf-8"?>
<Kids>
<Child>
<Name>Kid1</Name>
<FirstName>hisname</FirstName>
</Child>
<Child>
<Name>NewName</Name>
<FirstName>NewFirstName</FirstName>
</Child>
</Kids>
Update
Bug on save, you should set stream length to 0
Explanation
After reader existing file, stream does not remove any data
using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("YourKids.xml",FileMode.Open, store))
{
var x = XElement.Load(stream);
So when you call, data has been appended
x.Save(stream);
stream.Close();
Add stream.SetLength(0); before x.Save(stream); and all data will be overwritten.
Here is full version
if (store.FileExists("YourKids.xml"))
{
using (var stream = new IsolatedStorageFileStream("YourKids.xml", FileMode.Open,
store))
{
var x = XElement.Load(stream);
var t = new XElement("Child",
new XElement("Name", pName),
new XElement("FirstName", pFirstname)
);
x.Add(t);
stream.SetLength(0);
x.Save(stream);
stream.Close();
}
}
else
{
using (var CreateIsf = new IsolatedStorageFileStream("YourKids.xml", FileMode.Create, store))
{
var xDoc = new XElement("Kids",
new XElement("Child",
new XElement("Name", pName),
new XElement("FirstName", pFirstname)
)
);
xDoc.Save(CreateIsf);
CreateIsf.Close();
}
}
Please note: I've removed return statements as useless.
P.S. Take a look on resharper, it can improve code.

Related

How can I use condition and filters in LinkEntity?

I want to create a QueryExpression to simulate this SQL statement
select * from A
inner join B on A.b_id=B.ID
where B.Name like "% what ever %"
this is how the FetchXML might look like
<?xml version="1.0" encoding="UTF-8"?>
<fetch distinct="true" mapping="logical" output-format="xml-platform" version="1.0">
<entity name="A">
<attribute name="ID" />
<attribute name="sce_name" />
<link-entity name="B" alias="ab" to="b_id" from="A">
<filter type="and">
<condition attribute="Name" value="% what ever %" operator="like" />
</filter>
</link-entity>
</entity>
</fetch>
How I can make this in QueryExpression LinkQuery Conditions and Filters, also I don't want to start from B since A might have its conditions too.
This is what I have tried so far
QueryExpression query = new QueryExpression("A");
query.ColumnSet.AllColumns = true;
var link = new LinkEntity()
{
JoinOperator = JoinOperator.Inner,
EntityAlias = "c",
LinkFromEntityName = "A",
LinkToEntityName = "B",
LinkFromAttributeName = "b_id",
LinkToAttributeName = "ID",
};
using (var Service = new OrganizationService("con"))
{
EntityCollection entities = Service.RetrieveMultiple(query);
}
Hopefully this should be self explanatory.
QueryExpression query = new QueryExpression("a") //Start on A
{
ColumnSet = new ColumnSet(), //Columns to retrieve from A
Criteria = new FilterExpression(LogicalOperator.And) //Conditions for A
{
Conditions =
{
new ConditionExpression()
}
},
LinkEntities =
{
//Link to B
new LinkEntity("a", "b", "aid", "bid", JoinOperator.Inner)
{
Columns = new ColumnSet(), //Columns to retrieve from B
LinkCriteria = new FilterExpression() //Conditions for B
{
Conditions =
{
new ConditionExpression()
}
}
}
}
};
In addition to James' answer, don't forget you can also query using the fetch statement you already have:
RetrieveMultipleRequest fetchRequest1 = new RetrieveMultipleRequest
{
Query = new FetchExpression(
#"<fetch distinct="true" mapping="logical" output-format="xml-platform" version="1.0">
<entity name="A">
...
</entity>
</fetch>");
};
I rarely bother writing out QueryExpressions because executing with fetch is much easier.

Trying to understand how to conditionally write out element/attributes with LIINQToXML

This is the first time that I created a XML document using LINQToXML.
I am trying to understand how can I conditionally create attributes(or elements) when creating my document?
In this example a given car may/may not have a feature to it, so in that case I would not want to create that element, I also may have certain attributes in the feature node that could be missing. How could I handle these scenarios?
XDocument xDoc = new XDocument(
new XElement("root",
new XElement("NodeA"),
new XElement("Cars",
from p in listCars
select new XElement("Car", new XAttribute("name", p.CarName),
new XElement("Feature", new XAttribute("door", p.Door), new XAttribute("model", p.Model))
)
)
)
);
Desired result #1 (All features are missing for a given car):
<root>
<NodeA />
<Cars>
<Car name="Honda">
<Feature door="4" model="Accord" />
</Car>
<Car name="Ford" />
</Cars>
</root>
Desired result #2 (Some features could exist)
<root>
<NodeA />
<Cars>
<Car name="Honda">
<Feature door="4" model="Accord" />
</Car>
<Car name="Ford">
<Feature model="Focus" />
</Car>
</Cars>
</root>
2 seperate solutions in here. Either use a method to create the features node, or do it all in one:
static void Main(string[] args)
{
var listCars = new List<Car>();
listCars.Add(new Car { CarName = "test 1", Door = "0", Model = "" });
listCars.Add(new Car { CarName = "test 2", Door = "", Model = "" });
listCars.Add(new Car { CarName = "test 3", Door = "0", Model = "0" });
XDocument xDoc2 = new XDocument(
new XElement("root",
new XElement("NodeA"),
new XElement("Cars",
from p in listCars
select new XElement("Car",
new XAttribute("name", p.CarName),
p.Door != "" || p.Model != "" ?
new XElement("Feature",
p.Door != "" ? new XAttribute("door", p.Door) : null,
p.Model != "" ? new XAttribute("model", p.Model) : null) : null
)
)
)
);
XDocument xDoc = new XDocument(
new XElement("root",
new XElement("NodeA"),
new XElement("Cars",
from p in listCars
select new XElement("Car",
new XAttribute("name", p.CarName),
CreateFeature(p)
)
)
)
);
}
static XElement CreateFeature(Car p)
{
var el = new XElement("Feature",
p.Door != "" ? new XAttribute("door", p.Door) : null,
p.Model != "" ? new XAttribute("model", p.Model) : null);
return !el.Attributes().Any() ? null : el;
}
If you supply null instead of an element, it will be ignored, so you can use constructs like the following.
p.CarName != null ? new XAttribute("name", p.CarName) : null
If you're using C# 6, you can use null propagation.

rss feed retrieval using LINQ

I have a rss feed which is below format.
<response>
<results>
<game>
<image>
<icon_url>
<![CDATA[
http://-------------------
]]>
</icon_url>
<medium_url>
<![CDATA[
http://----
]]>
</medium_url>
</image>
</game>
</results>
</response>
Now, I want to retrieve the medium_url using LINQ.
var items = from item in rssFeed.
Elements("response").Elements("results").Elements("game")
select new
{
Image1 = item.Element("image").Element("medium_url").value
}
This doesn't seem to work.
You just need to make a small change:
var items = from item in rssFeed.
Elements("response").Elements("results").Elements("game")
select new
{
Image1 = (string)item.Element("image").Element("medium_url")
};
or
var items = from item in rssFeed.
Elements("response").Elements("results").Elements("game")
select new
{
Image1 = item.Element("image").Element("medium_url").Value
};

How to display video from xml file?

Hi am using xml file given below,how can i get videos from xml file?
<Category name="Videos">
<article articleid="68">
<videourl>
<iframe src="http://player.vimeo.com/video/52375409?fullscreen=0" width="500" height="298" frameborder="0"></iframe>
</videourl>
</article>
</Category>
My Code is
XDocument loadedData = XDocument.Load("CountriesXML.xml");
var data = from query in loadedData.Descendants("Country")
select new CountryData
{
url = (string)query.Element("videourl").Elements("iframe").Single().Attribute("src").Value,
};
countryList = data.ToList();
but i got NullReferenceException error
var xdoc = XDocument.Load("CountriesXML.xml");
var videos = from f in xdoc.Descendants("iframe")
select new {
Src = (string)f.Attribute("src"),
Width = (int)f.Attribute("width"),
Height = (int)f.Attribute("height")
};
Or with your updated code:
var xdoc = XDocument.Load("CountriesXML.xml");
var data = from c in xdoc.Descendants("Category") // you have Category element
select new CountryData {
url = (string)c.Element("article") // there is also article element
.Element("videourl")
.Elements("iframe")
.Single().Attribute("src")
};

XSLT transformation adding unwanted Linefeed

Using Vs 2010 I am editing a xsl file which will be used for transformation to output an xml file. I am trying to output a carriage return (
) and that only. I don't want the linefeed character (x0A) to appear.
No matter what kind of stunt I do in the xsl, the outputted file always adds a LF to my CR (CRLF) and I don't want that. The reason for why I want only the CR is that the output is parsed by a secondary system that sends sms-messages, which requires only LF.
I'm wondering if the problem is in the xsl or the transformation process.
The stylesheet:
<xsl:stylesheet xmlns:l="http://schemas.something.no/2008/" version='1.0'>
<xsl:import href='LydiaDateFuncs.xsl'/>
<xsl:template match='/'>
<xsl:apply-imports/>
</xsl:template>
<xsl:output method="xml" encoding="utf-8" indent="no" />
<xsl:template match="l:WorkOrderOccurrenceBE">
<Message>
<Body>
Adding cr here<xsl:text>
</xsl:text><xsl:text>
</xsl:text>No dice.
</Body>
</Message>
</xsl:template>
The code for transformation:
public static XmlDocument TransformObject(string xslFilepath, object serObj)
{
XmlDocument result = null;
xslTransf.Load(xslFilepath, new XsltSettings { EnableScript = true }, newXmlUrlResolver());
result = TransformObject(xslTransf, serObj);
}
public static XmlDocument TransformObject(XslCompiledTransform xslTransform, object transformObject)
{
XmlDocument result = null;
if (xslTransform != null)
{
DataContractSerializer serializer = new DataContractSerializer(transformObject.GetType());
string serializedObjValue = null;
using (MemoryStream stream = new MemoryStream())
{
serializer.WriteObject(stream, transformObject);
serializedObjValue = (new UTF8Encoding()).GetString(stream.ToArray());
}
if (serializedObjValue != null)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(serializedObjValue);
XmlDeclaration xmldec = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null);
xmldoc.InsertBefore(xmldec, xmldoc.DocumentElement);
using (Stream stream = new MemoryStream())
{
xslTransform.Transform(xmldoc, null, stream);
stream.Position = 0;
using (StreamReader sr = new StreamReader(stream, Encoding.Unicode))
{
string xmlRes = sr.ReadToEnd();
result = new XmlDocument();
result.LoadXml(xmlRes);
}
}
}
}
return result;
}
Anybody been experiencing a similar issue?
One way to solve this problem is to use an XmlTextWriter in a respective overload of XslCompiledTransform.Transform()
This instnce of XmlTextWriter must have its Settings property set with an instance of XmlWriterSettings for which you have specified whatever newline characters you want as the value of the XmlWriterSettings.NewLineChars property.
See: http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.settings.aspx
and
http://msdn.microsoft.com/en-us/library/system.xml.xmlwritersettings.newlinechars.aspx

Resources