XML traversal by path in c# - c#

I have an XML like this I want to get Return/ReturnHeader not EliminationsConsolidatedReturn/ReturnHeader. I want to traverse the XML by a path like Return/ReturnHeader. I got this function in c#. The issue is when I set the path Return/ReturnHeader is also getting the ReturnHeader of all inside EliminationsConsolidatedReturn,ParentReturn and SubsidiaryReturn. Please help me fix this.
c#
private List<XElement> GetNode(XNamespace nameSpace, string path)
{
var names = path.Split("\\".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
var element = new List<XElement>() { XDocument.Descendants(NameSpace + names[0]).FirstOrDefault() };
for (int i = 1; i < names.Length; i++)
{
var name = names[i];
element = element.Descendants(XName.Get(name, nameSpace.ToString())).ToList();
}
return element;
}
XML
<?xml version="1.0" encoding="utf-8"?>
<Return returnVersion="2014v4.0" xmlns="http://www.irs.gov/efile" xmlns:efile="http://www.irs.gov/efile" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ReturnHeader binaryAttachmentCnt="45" subsidiaryReturnCount="18">
</ReturnHeader>
<EliminationsConsolidatedReturn>
<ReturnHeader>
</ReturnHeader>
</EliminationsConsolidatedReturn>
<ParentReturn>
<ReturnHeader>
</ReturnHeader>
</ParentReturn>
<SubsidiaryReturn>
<ReturnHeader>
</ReturnHeader>
</SubsidiaryReturn>
</Return>

private List<XElement> GetNode(XNamespace nameSpace, string path)
{
var names = path.Split("\\".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
var element = new List<XElement>() { XDocument.Descendants(NameSpace + names[0]).FirstOrDefault() };
for (int i = 1; i < names.Length; i++)
{
var name = names[i];
element = element.Elements().Where(q => q.Name.LocalName == name).ToList();
}
return element;
}

Related

Convert a XML to CSV using C#

How to convert a XML file to a CSV file in C#, showing only these Tags: <original-impot-no>, <price>, <Small-price>, <Big-price>?
sample XML code: it represents one line among several lines, and every line may contains several <product-lineitem>Tags
<?xml version="1.0" encoding="UTF-8"?>
<impots xmlns="http://www.google.com/xml/impot//20016-02-31">
<impot impot-no="W0110891258">
<impot-date>2017-12-10T22:33:35.000Z</impot-date>
<prop-by>Yallo</prop-by>
<original-impot-no>891258</original-impot-no>
<currency>EUR</currency>
<server-locale>Esp</server-locale>
<lax>gross</lax>
<current-impot-no>123358</current-impot-no>
<product-lineitems>
<product-lineitem>
<price>450</price>
<red>6.50</red>
<Small-price>39</Small-price>
<Big-price>3229</Big-price>
<lineitem-text>Grand create</lineitem-text>
<basis>234.00</basis>
</product-lineitem>
</product-lineitems>
<product-lineitem>
<price>432</price>
<red>12</red>
<Small-price>44</Small-price>
<Big-price>34</Big-price>
<lineitem-text>Small create</lineitem-text>
<basis>44.00</basis>
</product-lineitem>
</product-lineitems>
</impot>
</impots>
I should get someting like this in y CSV file:
891258;450;39;229
891258;432;44;34
the C# code:
the problem that Im facing with this code is that I can not retrieve the descandent of the TAG <impot>
XmlTextReader xtr = new XmlTextReader(#"C:\Temp_Convert\Impot.xml");
StringBuilder dataToBeWritten = new StringBuilder();
while (xtr.Read())
{
if (xtr.NodeType == XmlNodeType.Element && xtr.Name == "original-impot-no")
{
string s1 = xtr.ReadElementString();
dataToBeWritten.Append(s1);
dataToBeWritten.Append(";");
}
else if (xtr.NodeType == XmlNodeType.Element && xtr.Name == "price")
{
string s2 = xtr.ReadElementString();
dataToBeWritten.Append(s2);
dataToBeWritten.Append(";");
}
else if (xtr.NodeType == XmlNodeType.Element && xtr.Name == "Small-price")
{
string s2 = xtr.ReadElementString();
dataToBeWritten.Append(s2);
dataToBeWritten.Append(";");
dataToBeWritten.Append(0);
dataToBeWritten.Append(Environment.NewLine);
}
else if (xtr.NodeType == XmlNodeType.Element && xtr.Name == "Big-price")
{
string s2 = xtr.ReadElementString();
dataToBeWritten.Append(s2);
dataToBeWritten.Append(";");
dataToBeWritten.Append(0);
dataToBeWritten.Append(Environment.NewLine);
}
}
File.WriteAllText(#"C:\Temp_Convert\Impot.csv", dataToBeWritten.ToString());
}
Can somebody please propose a solution, thank you so much in advance.
You have an invalid XML. I guess here is the correct format.
<?xml version="1.0" encoding="UTF-8"?>
<impots xmlns="http://www.google.com/xml/impot//20016-02-31">
<impot impot-no="W0110891258">
<impot-date>2017-12-10T22:33:35.000Z</impot-date>
<prop-by>Yallo</prop-by>
<original-impot-no>891258</original-impot-no>
<currency>EUR</currency>
<server-locale>Esp</server-locale>
<lax>gross</lax>
<current-impot-no>123358</current-impot-no>
<product-lineitems>
<product-lineitem>
<price>450</price>
<red>6.50</red>
<Small-price>39.00</Small-price>
<Big-price>3229.00</Big-price>
<lineitem-text>Grand create</lineitem-text>
<basis>234.00</basis>
</product-lineitem>
</product-lineitems>
<product-lineitems>
<product-lineitem>
<price>432</price>
<red>12</red>
<Small-price>44.00</Small-price>
<Big-price>34.00</Big-price>
<lineitem-text>Small create</lineitem-text>
<basis>44.00</basis>
</product-lineitem>
</product-lineitems>
</impot>
</impots>
You couldn't retrieve the descendant because you are not including the namespace http://www.google.com/xml/impot//20016-02-31.
Here is how the code should be.
XNamespace ns = "http://www.google.com/xml/impot//20016-02-31";
var results = xDocument.Descendants(ns + "impot");
Then, you need to modify your query to retrieve elements that you need.
Here is the sample. I assume that product-lineitems only has one child product-lineitem.
var results = xDocument.Descendants(ns + "impot").Select(x => new {
ImpotNo = x.Attribute("impot-no")?.Value,
ProductLineItems = x.Descendants(ns + "product-lineitems").Select(y => new
{
Item = y.Descendants(ns + "product-lineitem").Select(z => new
{
Price = z.Element(ns + "price")?.Value,
SmallPrice = z.Element(ns + "Small-price")?.Value,
BigPrice = z.Element(ns + "Big-price")?.Value,
}).FirstOrDefault()
})
});
foreach (var result in results)
{
foreach (var productLine in result.ProductLineItems)
{
dataToBeWritten.Append(result.ImpotNo);
dataToBeWritten.Append(";");
dataToBeWritten.Append(productLine.Item.Price);
dataToBeWritten.Append(";");
dataToBeWritten.Append(productLine.Item.SmallPrice);
dataToBeWritten.Append(";");
dataToBeWritten.Append(productLine.Item.BigPrice);
dataToBeWritten.Append(";");
dataToBeWritten.Append(0);
dataToBeWritten.Append(Environment.NewLine);
}
}
Well, first I tried to reformat your XML to be a bit more readable but the tag structure still seems wrong...
<impots
xmlns="http://www.google.com/xml/impot//20016-02-31">
<impot impot-no="W0110891258">
<impot-date>2017-12-10T22:33:35.000Z</impot-date>
<prop-by>Yallo</prop-by>
<original-impot-no>891258</original-impot-no>
<currency>EUR</currency>
<server-locale>Esp</server-locale>
<lax>gross</lax>
<current-impot-no>123358</current-impot-no>
<product-lineitems>
<product-lineitem>
<price>450</price>
<red>6.50</red>
<Small-price>39.00</Small-price>
<Big-price>3229.00</Big-price>
<lineitem-text>Grand create</lineitem-text>
<basis>234.00</basis>
-
</product-lineitems>
-
</product-lineitem>
<product-lineitems>
<product-lineitem>
<price>432</price>
<red>12</red>
<Small-price>44.00</Small-price>
<Big-price>34.00</Big-price>
<lineitem-text>Small create</lineitem-text>
<basis>44.00</basis>
</product-lineitems>
</product-lineitem>
Nonetheless, I'm guessing this line is incorrect since "impot-no" is an attribute...
impot-no = (string)x.Element("impot impot-no")
Perhaps you meant that line to be...
impot-no = (string)x.Attribute("impot-no").Value
Going by memory -- hopefully that is the correct way to retrieve an attribute.
Review the following code. Note the use of SelectMany to get the impot items to construct the desired object models.
XNamespace ns = "http://www.google.com/xml/impot//20016-02-31";
var results = xDocument.Descendants(ns + "impot")
.SelectMany(impot => impot.Descendants(impot.Name.Namespace + "product-lineitem")
.Select(item => new {
ImpotNo = (string)impot.Element(impot.Name.Namespace + "original-impot-no"),
Price = (string)item.Element(item.Name.Namespace + "price"),
SmallPrice = (string)item.Element(item.Name.Namespace + "Small-price"),
BigPrice = (string)item.Element(item.Name.Namespace + "Big-price"),
})
).ToList();
for (int i = 0; i < results.Count; i++) {
dataToBeWritten.Append(results[i].ImpotNo);
dataToBeWritten.Append(";");
dataToBeWritten.Append(results[i].Price);
dataToBeWritten.Append(";");
dataToBeWritten.Append(results[i].SmallPrice);
dataToBeWritten.Append(";");
dataToBeWritten.Append(results[i].BigPrice);
dataToBeWritten.Append(";");
dataToBeWritten.Append(0);
dataToBeWritten.Append(Environment.NewLine);
}
Also note the syntax used for the properties.

Declaring multiple strings from code

I have something like:
XmlDocument doc = new XmlDocument();
doc.Load(#"D:\filter.xml");
string filter1 = doc.SelectSingleNode("filter/f1").InnerText;
string filter2 = doc.SelectSingleNode("filter/f2").InnerText;
string filter3 = doc.SelectSingleNode("filter/f3").InnerText;
string filter4 = doc.SelectSingleNode("filter/f4").InnerText;
string filter5 = doc.SelectSingleNode("filter/f5").InnerText;
string filter6 = doc.SelectSingleNode("filter/f6").InnerText;
And so on...My question is how could I generate these strings in a loop?something like.
XmlDocument doc = new XmlDocument();
doc.Load(#"D:\filter.xml");
for (int i = 0; i < 7; i++)
{
string filter + i = doc.SelectSingleNode("filter/f" + i).InnerText;;
}
Fill a List<string>:
List<string> filterList = new List<string>();
for (int i = 0; i < 7; i++)
{
filterList.Add(doc.SelectSingleNode("filter/f" + i).InnerText);
}
Now you can access them via index, f.e. filter 5:
string filter5 = filterList[4]; // zero based
You want to use a collection, like List<string>:
XmlDocument doc = new XmlDocument();
doc.Load(#"D:\filter.xml");
var myList = new List<strinig>;
for (int i = 0; i < 7; i++)
{
myList.Add(doc.SelectSingleNode("filter/f" + i).InnerText);
}
Then you can use the list by referencing a string's index:
myValue = myList[3];
Use Collection to store String and this is how you can add string to collection from the XML.
doc.Load(#"D:\filter.xml");
List<string> filter = new List<string>();
foreach (XmlNode item in doc.SelectSingleNode("filter").ChildNodes)
{
filter.Add(item.InnerText.ToString());
}
Hope this helps.

C# (Xamarin): looping through XML

I have a Xamarin (C#) project, where I am trying to loop through some XML, but for some reason my code is not working.
This is what I have now:
DeviceList = new List<DeviceInfo>();
string ResultStatus = "";
string ResultDevice = "";
var result = Encoding.Default.GetString(e.Result);
result = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + result;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(result);
string xPathStatus = "ed_listdevices";
var nodes = xmlDoc.SelectNodes(xPathStatus);
foreach (XmlNode xNode in nodes) {
ResultStatus = xNode.SelectSingleNode("//status").InnerText;
ResultDevice = xNode.SelectSingleNode("//device").InnerText;
}
if (ResultStatus.ToLower() == "ok") {
XmlDocument deviceDoc = new XmlDocument();
deviceDoc.LoadXml(result);
var deviceNodes = deviceDoc.SelectNodes(xPathStatus + "/device");
//foreach(XmlNode dNode in deviceNodes) {
for (int i = 0; i < deviceNodes.Count; i++) {
DeviceList.Add(new DeviceInfo() {
DeviceID = deviceNodes[i].SelectSingleNode("//id").InnerXml,
DeviceName = deviceNodes[i].SelectSingleNode("//name").InnerXml,
DeviceExtraName = "",
DeviceOnlineStatus = deviceNodes[i].SelectSingleNode("//status").InnerXml,
Location = deviceNodes[i].SelectSingleNode("//address").InnerXml,
Time = deviceNodes[i].SelectSingleNode("//time").InnerXml
});
}
}
When I step though the code I get the "ResultStatus" and "ResultDevice" correctly, and when I get to the
for (int i = 0; i < deviceNodes.Count; i++)
the "deviceNodes" variable have a count of 91, and I can see all the individual xml elements that I am suppose to get from the webservice I am calling.
However, when I loop through deviceNodes[i] I only get values from the very first XML element (yes, the value of "i" does change). In other words, my DeviceList is filled with 91 entries with the same values.
Am I missing something obvious?? What am I doing wrong since this isn't working??
PS: I have also tried using a foreach (XmlNode node in deviceNodes) but the result was the same.
"//" in the selector tells it to search from the root node of the document. If you want to search locally under the "current" node, remove the "//"

To return the multiple values from for loop

I have parsed the xml document and used a for loop to loop for getting different values in string, but when I try to return the value I get only the last value obtained, I want to return all the individual values so that I can store that values in any file format,
Below is my code,
XmlDocument xmlDOC = new XmlDocument();
xmlDOC.LoadXml(periodID_Value_Before_OffSet); // string storing my XML
var value = xmlDOC.GetElementsByTagName("value");
var xmlActions = new string[value.Count];
string values = "";
string Period1 = "";
string periodlevel_period1 = "";
var listOfStrings = new List<string>();
string modified_listofstrings = listOfStrings.ToString();
string arrayOfStrings = "";
for (int i = 0; i < value.Count; i++)
{
var xmlAttributeCollection = value[i].Attributes;
if (xmlAttributeCollection != null)
{
var action = xmlAttributeCollection["periodid"];
xmlActions[i] = action.Value;
values += action.Value + ",";
string vals = values.Split(',')[1];
string counts = values;
string[] periods = counts.Split(',');
Period1 = periods[i];
// periodlevel_period1 = Client.GetAttributeAsString(sessionId, Period1, "name", "");
modified_listofstrings = Client.GetAttributeAsString(sessionId, Period1, "name", "");
modified_listofstrings.ToArray().ToString();
//listOfStrings = periodlevel_period1;
}
}
return modified_listofstrings;
This modified_listofstrings string only return last on value, I want to return the array of the values all obtained while looping.
----------Updated question----------
below is my Sample XMl
<string xmlns="http://tempuri.org/">
<ResultSetHierarchy totalResultsReturned="1" totalResults="1" firstIndex="0" maxCount="-1">
<object id="SC.1938773693.238">
<measure.values>
<series id="SC.1938773693.108280985">
<value periodid="SC.1938773693.394400760" value="17" />
<value periodid="SC.1938773693.1282504058" value="15" />
<value periodid="SC.1938773693.1631528570" value="13" />
</series>
</object>
</ResultSetHierarchy>
</string>
I want output as "SC.1938773693.394400760":"17" and so on for all periodid
Based on the provided information I have updated the answer.
List<string> items = new List<string>();
XmlDocument xmlDOC = new XmlDocument();
xmlDOC.Load(#"E:\Delete Me\ConsoleApplication1\ConsoleApplication1\bin\Debug\List.xml");
var elements = xmlDOC.GetElementsByTagName("value");
foreach (var item in elements)
{
XmlElement value = (XmlElement)item;
items.Add(string.Format("{0}:{1}", value.GetAttribute("periodid"), value.GetAttribute("value")));
}
It looks like you're trying to:
Load an XmlDocument
Get a list of all the attributes of name 'periodid'
Look each periodid up using a webservice call
Return a list of the lookup results
If that is correct, the following method should do what you need:
public List<string> GetListOfData()
{
XmlDocument xmlDOC = new XmlDocument();
xmlDOC.LoadXml("<Html><value periodid='Yabba'>YabbaValue</value><value periodid='Dabba'>DabbaValue</value><value periodid='Doo'>DooValue</value></Html>"); // string storing my XML
var value = xmlDOC.GetElementsByTagName("value");
var listOfStrings = new List<string>();
for (int i = 0; i < value.Count; i++)
{
var xmlAttributeCollection = value[i].Attributes;
if (xmlAttributeCollection != null)
{
var action = xmlAttributeCollection["periodid"];
string Period1 = action.Value;
listOfStrings.Add(QPR_webService_Client.GetAttributeAsString(sessionId, Period1, "name", "") + ":" + value[i].InnerText);
}
}
return listOfStrings;
}

XML to local database in windows phone 7

This function (look CODE) occur when xml file is downloaded from my server.. Now I want to load it in my local database...
XML
<?xml version="1.0" encoding="utf-8" ?>
<Questions>
<idQuestion>1</idQuestion>
<Question>Question 1</Question>
<CorrectAns>Ans1</CorrectAns>
<WrongAns1>Ans2</WrongAns1>
<WrongAns2>Ans3</WrongAns2>
<WrongAns3>Ans4</WrongAns3>
<Category>CategoryName</Category>
</Questions>
<Questions>
<idQuestion>2</idQuestion>
<Question>Question 2</Question>
<CorrectAns>Ans1</CorrectAns>
<WrongAns1>Ans2</WrongAns1>
<WrongAns2>Ans3</WrongAns2>
<WrongAns3>Ans4</WrongAns3>
<Category>CategoryName</Category>
</Questions>
etc...
CODE:
void downloader_Completed(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
return;
XElement quest = XElement.Parse(e.Result);
using (QuestionContext context = new QuestionContext(ConnectionString))
{
for (int i = 0; i < quest.Length; i++)
{
Question q = new Question();
q.idQuestion = ;
q.Question = ;
q.CorrectAns = ;
q.WrongAns1 = ;
q.WrongAns2 = ;
q.WrongAns3 = ;
context.QuestionsDB.InsertOnSubmit(q);
context.SubmitChanges();
}
}
}
I have 2 questions:
1)How to read the xml content and write them in:
q.idQuestion = ;
q.Question = ;
q.CorrectAns = ;
q.WrongAns1 = ;
q.WrongAns2 = ;
q.WrongAns3 = ;
2)for (int i = 0; i < quest.Length; i++) Herequest.Length give error (I know why)... How to make custom function to get the length? I need it to count <Questions> tags
Your xaml is not in valid format, because it has multiple root elements (Questions).
Assuming if it is in the correct format like this (I have modifiec)
string xmlData = #"<?xml version=""1.0"" encoding=""utf-8"" ?>
<Questions>
<Question>
<idQuestion>1</idQuestion>
<QuestionName>Question 1</QuestionName>
<CorrectAns>Ans1</CorrectAns>
<WrongAns1>Ans2</WrongAns1>
<WrongAns2>Ans3</WrongAns2>
<WrongAns3>Ans4</WrongAns3>
<Category>CategoryName</Category>
</Question>
<Question>
<idQuestion>2</idQuestion>
<QuestionName>Question 2</QuestionName>
<CorrectAns>Ans1</CorrectAns>
<WrongAns1>Ans2</WrongAns1>
<WrongAns2>Ans3</WrongAns2>
<WrongAns3>Ans4</WrongAns3>
<Category>CategoryName</Category>
</Question>
</Questions>";
You can have any number of tags as above
Now, you can convert this data into classes as shown below
XElement quest = XElement.Parse(xmlData);
var questionsData = from qn in quest.Descendants("Question")
select new Question
{
idQuestion = int.Parse(qn.Element("idQuestion").Value),
QuestionName = qn.Element("QuestionName").Value,
CorrectAns = qn.Element("CorrectAns").Value,
WrongAns1 = qn.Element("WrongAns1").Value,
WrongAns2 = qn.Element("WrongAns2").Value,
WrongAns3 = qn.Element("WrongAns3").Value
};
var Questions = questionsData.ToList();
var noofquestions = Questions.Count;
//If you want any question with specific id, say '2'
int idQuestion = 2;
var question = Questions.Where(item => item.idQuestion == idQuestion).First();
Here my Question class is like this
class Question
{
public int idQuestion ;
public string QuestionName ;
public string CorrectAns ;
public string WrongAns1 ;
public string WrongAns2 ;
public string WrongAns3 ;
}
Now apply the above process to your data, in whatever way applies

Categories

Resources