I feel this is really stupid but can't seem to figure it out.
I have a Combobox that populates from an XML file and works great.
Now I want to display a specific element (Description) when an item is selected, but the string always wants to return null.
Xml:
<?xml version="1.0" encoding="utf-8" ?>
<Barbarian>
<Special id="Lesser Ancestor Totem">
<SpecialName>Lesser Ancestor Totem</SpecialName>
<Description>Gain a +2 Insight Bonus to a skill (that you can use while raging) while raging</Description>
</Special>
</Barbarian>
Code to get the description and put into a RichTextBox:
public void LoadFeatDescription()
{
string Class = CharClassSelector.Text;
string Feat = FeatPicker.Text;
XDocument doc = XDocument.Load($"\\Xml\\Classes\\{Class}.xml");
string description = doc.XPathSelectElement($"//Special[#id='{Feat}']/Description").Value;
//the string description wants to stay null despite my efforts
DescriptionBox.Text = description;
}
The idea is that this will load a specific file and get the description element based on the id.
Am I missing something stupid?
Thanks!
Edit: added in the entire XML contents
I got it.
Changed string description = doc.XPathSelectElement($"//Special[#id='{Feat}']/Description").Value;
to
string description = (string)doc.XPathSelectElement($"//Special[#id='{Feat}']/Description").Value;
I knew it was something stupid...
Related
I'm working with a 3rd party application that takes input in via XML, and then returns the input back out in XML, I'm looking for a way to format the information to display it nicely in a richtextbox.
<Solution>
<ID>1</ID>
<Property>
<Name>DriverSheave</Name>
<Value>1VP34</Value>
</Property>
<Property>
<Name>DriverBushing</Name>
<Value>
</Value>
</Property>
<Property>
<Name>DrivenSheave</Name>
<Value>AK49</Value>
this is some sample xml that i would receive as an output from the 3rd party app, What I'm currently doing is this.
richTextBox1.Text = Configurator.Results.InnerText.ToString();
which gives me results like this.
1DriverSheave3MVP55B69DriverBushingDrivenSheave3MVB200RDrivenBushingR1BeltB225BeltQty3
essentially id like to know the best way to move these around, and make the output formatted nicely. so im not asking that you format this for me, but rather let me know the proper way to go about formatting this.
id like it to look similar to
Refer to the below code:
using System.XML.Linq;
class XMLParseProgram
{
public DataTable ReadXML(string strXML)
{
XDocument xdoc = XDocument.Load(strXML);
var property= from props in xdoc.Element("Solution").Elements("Property").ToList().ToList();
if (property!= null)
{
DataTable dtItem = new DataTable();
dtItem.Columns.Add("Name");
dtItem.Columns.Add("Value");
foreach (var itemDetail in property.ElementAt(0))
{
dtItem.Rows.Add();
if (itemDetail.Descendants("Name").Any())
dtItem.Rows[count]["Name"] = itemDetail.Element("Name").Value.ToString();
if (itemDetail.Descendants("Value").Any())
dtItem.Rows[count]["Value"] = itemDetail.Element("Value").Value.ToString();
}
}
}
}
You can use the DataTable to generate the string in your program the way you want.
If one of Configurator or Configurator.Results is an XmlDocument or XmlElement you can use one of these converters to an XElement:
richTextBox1.Text = Configurator.Results.ToXElement().ToString();
I am attempting to consume a Rest service that returns an XML response. I have successfully made the get request, my problem is processing the response. The response includes a namespace that seems to be messing up my linq query. I have tried almost everything I can think of userNames always comes up empty. Any help would be greatly appreciated and could possibly save my sanity.
<?xml version="1.0" encoding="UTF-8"?>
<tsResponse xmlns="http://tableausoftware.com/api" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tableausoftware.com/api http://tableausoftware.com/api/ts-api-2.0.xsd">
<users>
<user id="c9274ce9-0daa-4aad-9bd2-3b1d6d402119" name="_DevITTemplate" role="Unlicensed" publish="false" contentAdmin="false" lastLogin="" externalAuthUserId=""/>
string usersList =
request.DownloadString("http://bshagena-sandbox/api/2.0/sites/b4126fe9-d7ee-4083-88f9- a5eea1f40416/users/");
request.Dispose();
XDocument xd;
XElement users;
XNamespace ns = "http://tableausoftware.com/api";
xd = XDocument.Parse(usersList);
users = xd.Root.Elements().First();
var userNames = from a in users.Descendants(ns +"users")
select (string)a.Attribute("name").Value;
It is your user element which contains attribute name, not the names wrapper element. Adjust your xpath accordingly: (Your use of the XNamespace is fine)
var userNames = from a in users.Descendants(ns + "user")
select a.Attribute("name").Value;
Minor - Attribute.Value is already a string - no need to cast it :)
Here is what I did and it seemed to work. Sorry for all the comments. I am sure this is not the most efficient way to do this. I hope this helps someone else losing their mind over the same issue.
// Sends get request and stores response as a string
string usersList =
request.DownloadString("http://<serverName>/api/2.0/sites/b4126fe9-d7ee-4083-88f9-a5eea1f40416/users/");
// declares an XML document object
XDocument xd;
// Declares and XML element object
XElement users;
// Declares a stupid XML namespace object
XNamespace ns = "http://tableausoftware.com/api";
// Sets document to value of string
xd = XDocument.Parse(usersList);
// Sets the element to value of the first node of the xml document
users = xd.Root.Elements().First();
// Creates an array and queries elements based on attribute of name
var userNames = from a in users.Elements(ns + "user")
select (string)a.Attribute("name").Value;
<Customers>
<Customer1>
<Name>Bobby</Name>
<Age>21</Age>
<Address>Panjim</Address>
</Customer1>
<Customer2>
<Name>Peter</Name>
<Age>32</Age>
<Address>Panjim</Address>
</Customer2>
<Customer4>
<Name>Joel</Name>
<Age>32</Age>
<Address>Mapusa</Address>
</Customer4>
</Customers>
So the thing is I want to delete a particular element and when i delete the first element i.e customer1, I want to update the other elements. I mean I want to make customer3, customer2 and customer2, customer1.
Can anyone please help me achieve this?
What about:
class Program {
static void Main(string[ ] args) {
XDocument doc = XDocument.Load("D:\\file.xml"); //example file
doc.Root.SwitchAndRemove("Customer1");
doc.Save("D:\\file.xml");
}
}
public static class Utilities {
public static void SwitchAndRemove(this XElement customers, XName name) {
var x = customers.Descendants().Where(e => e.Name == name).Select((element, index) => new { element, index }).Single();
int count = 0;
XElement temp = x.element;
foreach (XElement el in customers.Nodes()) {
if (count == x.index + 1) {
temp.RemoveAll();
temp.Add(el.Descendants().ToArray());
temp = el;
}
else
count++;
}
temp.Remove();
}
}
By giving as input your xml the output is the following:
<?xml version="1.0" encoding="utf-8"?>
<Customers>
<Customer1>
<Name>Peter</Name>
<Age>32</Age>
<Address>Panjim</Address>
</Customer1>
<Customer2>
<Name>Joel</Name>
<Age>32</Age>
<Address>Mapusa</Address>
</Customer2>
</Customers>
I'd argue that your problem is not how you could rename your nodes with minimum effort but structure of your XML file.
You said order of customers is not important and apparently customer tag's number is not important, either, since you want to rename the tags upon deletion.
So maybe this structure just creates unnecessary complexity and extra work for you.
Only reason I see you could need the number in tag is to identify the node you are about to remove. Am I right or is there something more to it? If not then you could add random unique identifier (like Guid) to your customer data to remove the right one.
Could save you lot of trouble.
<customers>
<customer>
<guid>07fb-877c-...</guid>
<name>Notch</name>
<age>34</age>
<address>street</address>
</customer>
<customer>
<guid>1435-435a-...</guid>
<name>Sam</name>
<age>23</age>
<address>other</address>
</customer>
<customers>
Say the element you have to delete is Customer1, first of all you can read the complete xml file using one of the XML parsing classes available in c# like XDocument or XmlReader and write to another xml file say "Temp.xml" skipping the Customer1 element completely. This way we have achieved the deletion part.
Next to update, forget the file being XML file and read the entire file to a string, say "xmlstring". Now use the Replace function available with a string data type to replace "Customer2" with "Customer1" and then "Customer3" with "Customer2" and so on.
And now delete your original XML file and write the string "xmlstring" using a stream writer to a file name "YourFileName.xml"
Thats it. Hope this solution works for you. Try this and in case u are unable get this done, share the code which u tried and we shall suggest how to work it out.
taken from your comment that the order does not have to be preserved then you can do this
public static void RemoveCustomer(XElement customers, XElement removeThis){
var last = customeers.Elements().Last();
if(last != removeThis){
foreach(var element in removeThis.Elements()){
element.Value = last.Element(element.Name).Value;
}
}
last.Remove();
}
It effectively substitutes the one to be removed with the last (unless the last should be removed) and thereby eliminates the need for renaming any of the other elements
When i am doing Deserialize of xml i am getting "There is an error in XML document (1, 41)." . Can anyone tell me about what is the issue is all about.
public static T DeserializeFromXml<T>(string xml)
{
T result;
XmlSerializer ser = new XmlSerializer(typeof(T));
using (TextReader tr = new StringReader(xml))
{
result = (T)ser.Deserialize(tr);
}
return result;
}
I use this function to do it.
<?xml version='1.0' encoding='utf-16'?>
<Message>
<FirstName>Hunt</FirstName>
<LastName>DAvid</LastName>
</Message>
Ensure your Message class looks like below:
[Serializable, XmlRoot("Message")]
public class Message
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
This works for me fine:
string xml = File.ReadAllText("c:\\Message.xml");
var result = DeserializeFromXml<Message>(xml);
MSDN, XmlRoot.ElementName:
The name of the XML root element that is generated and recognized in
an XML-document instance. The default is the name of the serialized
class.
So it might be your class name is not Message and this is why deserializer was not able find it using default behaviour.
Agreed with the answer from sll, but experienced another hurdle which was having specified a namespace in the attributes, when receiving the return xml that namespace wasn't included and thus failed finding the class.
i had to find a workaround to specifying the namespace in the attribute and it worked.
ie.
[Serializable()]
[XmlRoot("Patient", Namespace = "http://www.xxxx.org/TargetNamespace")]
public class Patient
generated
<Patient xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.xxxx.org/TargetNamespace">
but I had to change it to
[Serializable()]
[XmlRoot("Patient")]
public class Patient
which generated to
<Patient xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
This solved my problem, hope it helps someone else.
First check the variables declared using proper Datatypes.
I had a same problem then I have checked, by mistake I declared SAPUser as int datatype so that the error occurred.
One more thing XML file stores its data using concept like array but its first index starts having +1.
e.g. if error is in(7,2) then
check for 6th line always.....
I had the same thing. All came down to a "d" instead of a "D" in a tag name in the schema.
In my case I had a float value expected where xml had a null value so be sure to search for float and int data type in your xsd map
On a WEC7 project I'm working on, I got a similar error. The file I was serializing in was serialized out from an array of objects, so I figured the XML was fine. Also, I have had this working for a few previous classes, so it was quite a puzzle.
Then I noticed in my earlier work that every class that I was serializing/deserializing had a default constructor. That was missing in my failed case so I added it and and voila... it worked fine.
I seem to remember reading somewhere that this was required. I guess it is.
Consider this XML file. Note the first Tutorial has an Author child element, and the second Tutorial does not:
<?xml version="1.0" encoding="utf-8" ?>
<Tutorials>
<Tutorial>
<Author>The Tallest</Author>
<Title>
WPF Tutorial - Creating A Custom Panel Control
</Title>
<Date>2/18/2008</Date>
</Tutorial>
<Tutorial>
<Title>
2nd WPF Tutorial - Creating A Custom Panel Control
</Title>
<Date>2/18/2008</Date>
</Tutorial>
</Tutorials>
How do I use LINQ-to-XML to load the data that is present? The code below blows up when it gets to the Tutorial section that lacks an author. I cannot figure out how to write the where statement to exclude the block that lacks an author, or how to make the code elegantly skip over the missing data. I have tried this:
where tutorial.Element("Title") != null
But the above has no effect.... Here is the problem code:
XDocument xmlDoc = XDocument.Load("C:\\xml\\2.xml");
var tutorials = from tutorial in xmlDoc.Descendants("Tutorial")
select new
{
Author = tutorial.Element("Author").Value,
Title = tutorial.Element("Title").Value,
Date = tutorial.Element("Date").Value,
};
foreach (var tutorial in tutorials)
{
Console.WriteLine("author: " + tutorial.Author);
Console.ReadKey();
}
Use the XElement to String conversion operator instead of the Value property:
var tutorials = from tutorial in xmlDoc.Root.Elements("Tutorial")
select new
{
Author = (string)tutorial.Element("Author"),
Title = (string)tutorial.Element("Title"),
Date = (DateTime)tutorial.Element("Date"),
};
Instead of referencing the Value property of the XElement that might be null, you can do an explicit cast to string instead, like this:
Author = (string) tutorial.Element("Author")
Check out this article for more info:
Improving LINQ Code Smell with Explicit and Implicit Conversion Operators