I have an xml with the following structure
<student>
<name fname="oliver">
</name>
<name fname="de">
</name>
<name fname="johnson">
</name>
</student>
my code is like this:
//after loading into an xmldocument called xmlrecord
XmlNode row = xmlRecord.SelectSingleNode("/student");
student.fname = row.SelectSingleNode("name[fname]");
But its not returning anything. Pls what is the best way to select the fname='johnson' node?
You want the fname attribute of the first <name> element, so you should write:
student.fname = row.SelectSingleNode("name/#fname");
Related
I want to get xml string by filtering data. For example, I need to filter some students whose sex is female.
I need to use linq to xml to get xml string.
The following is my initial xml code and expected xml string.
Initial xml code:
<? xml version="1.0" encoding="utf-8"?>
<School>
<Student>
<Name>Test1</Name>
<Birthday>1997-02-23</Birthday>
<Id>1001</Id>
<Sex>male</Sex>
<ClassId>01</ClassId>
<Scorevalue>Net Revenue</Scorevalue>
</Student>
<Student>
<Name>Test1</Name>
<Birthday>1998-02-21</Birthday>
<Id>1002</Id>
<Sex>female</Sex>
<ClassId>02</ClassId>
<Scorevalue>Net Revenue</Scorevalue>
</Student>
<Student>
<Name>Test1</Name>
<Birthday>1997-02-24</Birthday>
<Id>1004</Id>
<Sex>male</Sex>
<ClassId>03</ClassId>
<Scorevalue></Scorevalue>
</Student>
</School>
Expected xml string:
<School>
<Student>
<Name>Test1</Name>
<Birthday>1998-02-21</Birthday>
<Id>1002</Id>
<Sex>female</Sex>
<ClassId>02</ClassId>
<Scorevalue>Net Revenue</Scorevalue>
</Student>
</School>
Use the following code.
XDocument doc = XDocument.Load(path);
foreach(var x in doc.Descendants("Student").Where(x => x.Element("Sex").Value == "female"))
{
Console.WriteLine(x.ToString());
}
You can get the xml string like this,
string path = "D:\\test.xml";
XDocument doc = XDocument.Load(path);
doc.Descendants("Student").Where(x =>x.Element("Sex").Value=="male").Remove();
Console.WriteLine(doc.ToString());
I want to select a node where inner text of cat is 'PG' using XPath
<?xml version="1.0" encoding="utf-8"?>
<Students>
<student>
<name>Talha</name>
<cat>PG</cat>
</student>
<student>
<name>irfan</name>
<cat>UG</cat>
</student>
<student>
<name>Ali</name>
<cat>PG</cat>
</student>
<student>
<name>Umer</name>
<cat>UG</cat>
</student>
</Students>
Code which I tried is this
XmlElement xmldoc = (XmlElement)doc.DocumentElement
.SelectSingleNode("/Students/student/*[*[local-name()='cat']='PG']");
To select all the student nodes which sub-elements cat have the value 'PG' use this XPath expression
/Students/student[cat='PG']
To only get the first one use
/Students/student[cat='PG'][1]
So in the syntax of C# use
XmlElement xmldoc = (XmlElement)doc.DocumentElement.SelectSingleNode("/Students/student[cat='PG'][1]");
I have a XML which looks like:
<users>
<user id="0">
<name>John</name>
<lastName>Smith</lastName>
<bills>
<bill id="0">
<name>Water</name>
<forMonth>2013-12-01</forMonth>
<money>235</money>
<lastDayToPay>2014-01-02</lastDayToPay>
<payed>False</payed>
</bill>
<bill id="1">
<name>Telephone</name>
<forMonth>2013-11-01</forMonth>
<money>690</money>
<lastDayToPay>2014-01-01</lastDayToPay>
<payed>True</payed>
</bill>
</bills>
</user>
How can i add new bill for the user, i have problem accessing "bills" node and adding element to it. I'm using c#.
use following code
XmlDocument myDocument = new XmlDocument();
myDocument.Load(XMLFile);
XmlNode newNode = myDocument.CreateElement("bill");
//add values;
var requiredNode = myDocument.ChildNodes.OfType<XmlElement>().Where(o => o.Name == "bills").First();
requiredNode.AppendChild(newNode);
myDocument.Save(XMLFile);
I have a trouble reading from XML file using LINQ.
Here is my XML file
<?xml version="1.0" encoding="utf-8"?>
<Employees>
<Employee>
<Name Type="First">Jack</Name>
<Name Type="Last">Black</Name>
</Employee>
<Employee>
<Name Type="First">John</Name>
<Name Type="Last">Blue</Name>
</Employee>
<Employee>
<Name Type="First">Dan</Name>
<Name Type="Last">Red</Name>
</Employee>
<Employee>
<Name Type="First">Patrick</Name>
<Name Type="Last">Green</Name>
</Employee>
</Employees>
The code I am using is following
XElement doc = XElement.Load("xmldoc.xml");
var query = from x in doc.Elements("Employee") where x.Element("Name").Attribute("Type").Value == "First" select x;
foreach (XElement item in query)
{
Console.WriteLine(item.Element("Name").Value);
}
This code returns me all first names but when i change attribute value from first to last it comes blank.
When i switch name nodes it retuns last names. For me it looks like for each employe query returns values from first name node and ignoring the second one. Could you please help me fix this?
The problem is that the x.Element("Name") call will return the first Name element. You actually need to query all the Name elements and filter for the one with the Last attribute value.
Try this instead:
var query = from x in doc.Elements("Employee").Elements("Name")
where x.Attribute("Type").Value == "Last"
select x;
foreach (XElement item in query)
{
Console.WriteLine(item.Value);
}
I have two XML documents.
My objective is to replace one of the nodes in the first document with the entire contents of the second Xml documents.
So the first document - Parent looks something like this:
<Root>
<AgencyName = "Some Agency"/>
<Originator = "Some other Agency"/>
<Type = "AnonymousType"/>
<Details/>
</Root>
The second document - children looks like this:
<Root>
<Details>
<Detail1>
...
</Detail1>
<Detail2>
...
</Detail2>
<Detail3>
...
</Detail3>
</Details>
</Root>
The node <Details/> has to be replaced with the contents of the second document.
I am attempting to use Linq to XML to do this. The first document is represented in an XDocument class and the second one is represented in an XElement class. There are several child attributes for <Detail/> and I haven't listed them here.
I am attempting to replace the element from the first document with this XElement class.
If I try something like this,
ParentDoc.Element("Details").ReplaceAll(children);
it is unlikely to work. How should I do the replace?
var doc = XDocument.Load(#"C:\Tools\test.xml");
var doc2 = XDocument.Load(#"C:\Tools\test2.xml");
var children = doc2.Root.Element("Details");
var parentNode = doc.Root.Element("Details");
parentNode.ReplaceWith(children);
By the way, your xml are not correct, so you'll get exceptions.
I tried with
<Root>
<AgencyName name= "Some Agency"/>
<Originator name= "Some other Agency"/>
<Type name= "AnonymousType"/>
<Details/>
</Root>
and
<Root>
<Details>
<Detail1>
asdf
</Detail1>
<Detail2>
asde
</Detail2>
<Detail3>
eere
</Detail3>
</Details>
</Root>
and got
<?xml version="1.0" encoding="utf-8"?>
<Root>
<AgencyName name="Some Agency" />
<Originator name="Some other Agency" />
<Type name="AnonymousType" />
<Details>
<Detail1>
asdf
</Detail1>
<Detail2>
asde
</Detail2>
<Detail3>
eere
</Detail3>
</Details>
</Root>