SqlDataReader not producing XML document - c#

I am trying to dynamically create a XML for a webservice but when I test the service I get the following error
XML Parsing Error: no element found
Location: http://stuiis.cms.gre.ac.uk/dd615/aspweb/WatCoursework/Service.asmx/getMusicdetailsSql
Line Number 1, Column 39:
--------------------------------------^
// Make a new XML document in memory.
XmlDocument doc = new XmlDocument();
// Fill this document with a root element
// named <Inventory>.
XmlElement musicInformation = doc.CreateElement("musicInformation");
using (SqlDataReader oDr = myCommand.ExecuteReader())
{
while (oDr.Read())
{
// Now, make a sub element named <Car> with
// an ID attribute.
XmlElement musicdetails = doc.CreateElement("musicdetails");
musicdetails.SetAttribute("m_id", oDr["m_id"].ToString());
// Build the data within the <Car> element.
XmlElement p_id = doc.CreateElement("p_id");
p_id.InnerText = oDr["p_id"].ToString();
XmlElement artistname = doc.CreateElement("artistname");
artistname.InnerText = oDr["artistname"].ToString();
XmlElement recordname = doc.CreateElement("recordname");
recordname.InnerText = oDr["recordname"].ToString();
XmlElement recordtype = doc.CreateElement("recordtype");
recordtype.InnerText = oDr["recordtype"].ToString();
XmlElement format = doc.CreateElement("format");
format.InnerText = oDr["format"].ToString();
XmlElement price = doc.CreateElement("price");
price.InnerText = oDr["price"].ToString();
musicdetails.AppendChild(p_id);
musicdetails.AppendChild(artistname);
musicdetails.AppendChild(recordname);
musicdetails.AppendChild(recordtype);
musicdetails.AppendChild(format);
musicdetails.AppendChild(price);
musicInformation.AppendChild(musicdetails);
}
return doc;
}

I think you forgot to add the musicInformation to the document:
}
doc.AppendChild(musicInformation);
return doc;
}

Related

'This document already has a 'DocumentElement' node.'

I am trying to create a xml document of following format
<TemplateID>xxxxx</TemplateID>
<CaptionOptions>
<CaptionField>
<Field>xxx</Field>
<Text>xxx</Text>
</CaptionField>
<CaptionField>
<Field>xxxx</Field>
<Text>""</Text>
</CaptionField>
</CaptionOptions>
Here is the code that I wrote
XmlDocument xml2 = new XmlDocument();
XmlElement e = xml2.CreateElement("TemplateID");
e.InnerText = "xxxx";
xml2.AppendChild(e);
XmlElement root2 = xml2.CreateElement("CaptionOptions");
xml2.AppendChild(root2); //error here
XmlElement child2a = xml2.CreateElement("CaptionField");
root2.AppendChild(child2a);
XmlElement child2aa = xml2.CreateElement("Field");
child2a.InnerText = "xxxx";
XmlElement child2ab = xml2.CreateElement("Text");
child2a.InnerText = "xxxx";
child2a.AppendChild(child2aa);
child2a.AppendChild(child2ab);
child2a.AppendChild(child2aa);
child2a.AppendChild(child2ab);
My real code was different from the one I was trying to ask earlier....
You could use
XmlElement child = xml.CreateElement("Players");
child.SetAttribute("Nationality", "England");
child.InnerText = "Rooney";
You need to create attributes and append them to the Player element. But your xml hierarchy doesn't look right.
As discussed, now edited.
XmlDocument doc = new XmlDocument();
XmlElement template = doc.CreateElement("Template");
XmlNode id = doc.CreateElement("TemplateID");
id.InnerText = "123456";
template.AppendChild(id);
doc.AppendChild(template);
XmlElement options = doc.CreateElement("CaptionOptions");
XmlElement captionField = doc.CreateElement("CaptionField");
XmlElement field1 = doc.CreateElement("Field");
field1.InnerText = "Field1Text";
XmlElement text1 = doc.CreateElement("Field");
text1.InnerText = "Text1Text";
captionField.AppendChild(field1);
captionField.AppendChild(text1);
options.AppendChild(captionField);
template.AppendChild(options);
string xml = doc.OuterXml;
Hope that helps.

Reading the xml element content and attribute at the same time with c#

What I'm trying to do is exactly:
i can read xml element's value and attribute in same time.
My English is not very good. Sorry for this.
<all>
<hp id="1" t="K" k="1">DÖNEN VARLIKLAR</hp>
<hp id="10" t="K" k="10">HAZIR DEĞERLER</hp>
<hp id="100" t="K" k="100">KASA</hp>
<hp id="1" t="B" k="100.02">ŞUBE KASASI</hp>
<hp id="5417" t="B" k="100.Y0001">Yeni Hesap Adı</hp>
</all>
this my xml file.
i want to read hp element's value and k attribute and I want to put the values I read in rows under the columns in datagridview.This my c# codes:
dataGridView2.ColumnCount = 2;
dataGridView2.Columns[0].Name = "Hesap Kodu";
dataGridView2.Columns[1].Name = "Hesap Adı";
XDocument xDoc = XDocument.Load(#"C:\Luca_Offline_Yerel\hp461979110642727.xml");
XElement rootElement = xDoc.Root;
XmlDocument i = new XmlDocument();
i.Load(#"C:\Luca_Offline_Yerel\hp461979110642727.xml");
XmlNodeList xnList = i.SelectNodes("/all / hp");
DataSet ds = new DataSet();
//xml dosyamızı okumak için bir reader oluşturuyoruz.
XmlReader xmlFile;
xmlFile = XmlReader.Create(#"C:\Luca_Offline_Yerel\hp461979110642727.xml", new XmlReaderSettings());
//içeriği Dataset e aktarıyoruz.
ds.ReadXml(xmlFile);
//datagridviewin kaynağı olarak dataseti gösteriyoruz.
String Kod, Ad;
foreach (XElement rehberimiz in rootElement.Elements())
{
//yapmam gereken ad'a hp nin element içeriğini ekleme!
Kod = rehberimiz.Attribute("k").Value;
Ad = rehberimiz.Element("hp_Text").Value;
dataGridView2.Rows.Add(Kod, Ad);
}
but i get an error on this line:
Ad = rehberimiz.Element("hp_Text").Value;
Do like this:
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load("XMLFile1.xml");
XmlNode oRootNode = xmlDocument.SelectSingleNode("/all");
foreach (XmlNode item in oRootNode.ChildNodes)
{
var Kod = item?.Attributes["k"]?.InnerText;
var Ad = item?.InnerText;
Console.WriteLine(Kod + " " + Ad);
}
Add Kod and Ad to Row. This will resolve your problem.
You should use FirstNode.ToString() instead of Element("hp_Text").Value:
String Kod, Ad;
foreach (XElement rehberimiz in rootElement.Elements())
{
//yapmam gereken ad'a hp nin element içeriğini ekleme!
Kod = rehberimiz.Attribute("k").Value;
Ad = rehberimiz.FirstNode.ToString();
dataGridView2.Rows.Add(Kod, Ad);
}

Child node is repeating, can't get it to work

i have an xml code that create an xml file and this works great however i am a bit confused on why one of the parent node still opens and it does not group by employee first ( the employee hours and workdays should only open up once as long as the employee id is the same and close at the end when all days have been created) see attached code `
// Create a new <Employees> element and add it to the root node
XmlElement Employees = xmlDoc.CreateElement("employees");
xmlDoc.DocumentElement.AppendChild(Employees);
// Create a new <staffingHours> element and add it to the root node
XmlElement parentNode = xmlDoc.CreateElement("CompanyHours");
// Set attribute name and value!
parentNode.SetAttribute("processType", "merge");
xmlDoc.DocumentElement.PrependChild(parentNode);
string catid = "";
string nurseCode = GridView1.Rows[0].Cells[0].Text;
foreach ( GridViewRow row in GridView1.Rows)
{
//first part of EMPLOYEES ELEMENTS AND CHILD ELEMENTS
string fromFormat = "MM/dd/yyyy";
string toFormat = "yyyy-MM-dd";
if (catid != row.Cells[0].Text)
{
XmlElement employee = xmlDoc.CreateElement("employee");
xmlDoc.DocumentElement.AppendChild(employee);
Employees.AppendChild(employee);
//create the element
XmlElement NurseId1 = xmlDoc.CreateElement("employeeId");
employee.AppendChild(NurseId1);
NurseId1.InnerText = row.Cells[0].Text;
XmlElement HireDate1 = xmlDoc.CreateElement("hireDate");
employee.AppendChild(HireDate1);
DateTime newdate = DateTime.ParseExact(row.Cells[6].Text, fromFormat, null);
HireDate1.InnerText = newdate.ToString(toFormat);//row.Cells[6].Text;
xmlDoc.DocumentElement.InsertAfter(Employees, xmlDoc.DocumentElement.LastChild);
}
XmlElement EmployeeHours = xmlDoc.CreateElement("EmployeeHours");
if (catid != row.Cells[0].Text)
{
XmlElement NurseId = xmlDoc.CreateElement("employeeId");
staffHours.AppendChild(NurseId);
NurseId.InnerText = row.Cells[0].Text;
}
XmlElement WorkDays = xmlDoc.CreateElement("workDays");
XmlElement WorkDay = xmlDoc.CreateElement("workDay");
//Third node and data source
XmlElement Date = xmlDoc.CreateElement("date");
WorkDay.AppendChild(Date);
DateTime converteddate = DateTime.ParseExact(row.Cells[1].Text,
fromFormat, null);
Date.InnerText = converteddate.ToString(toFormat);
XmlElement hourEntries = xmlDoc.CreateElement("hourEntries");
xmlDoc.DocumentElement.PrependChild(hourEntries);
WorkDay.AppendChild(hourEntries);
XmlElement HourEntry = xmlDoc.CreateElement("hourEntry");
xmlDoc.DocumentElement.PrependChild(HourEntry);
hourEntries.AppendChild(HourEntry);
//Fourth node and data source
XmlElement Hours = xmlDoc.CreateElement("hours");
HourEntry.AppendChild(Hours);
Hours.InnerText = row.Cells[2].Text;
XmlElement JobTitleCode = xmlDoc.CreateElement("jobTitleCode");
HourEntry.AppendChild(JobTitleCode);
JobTitleCode.InnerText = row.Cells[3].Text;
XmlElement payTypeCode = xmlDoc.CreateElement("payTypeCode");
HourEntry.AppendChild(payTypeCode);
payTypeCode.InnerText = row.Cells[4].Text;
staffHours.AppendChild(WorkDays);
WorkDays.AppendChild(WorkDay);
parentNode.AppendChild(EmployeeHours);
xmlDoc.DocumentElement.InsertAfter(parentNode,
xmlDoc.DocumentElement.LastChild);
catid = row.Cells[0].Text;}
now once this code runs it create the attached image but i don't want the highlighted lines repeating inside the same parent node...
[1]: https://i.stack.imgur.com/eirsv.png
My issue was the element was inside of my loop i needed a new set of eyes to look into this. Good catch Tamas Szabo. issue is resolved.

Can't get attribute value with C# XPath

I've spent so much time with this already, still can't get the value of NTE attribute. Can someone please help?
C#
StreamReader sr = new StreamReader(resp.GetResponseStream());
XPathDocument xmlDoc = new XPathDocument(sr); // holds xml document
XPathNavigator xmlNav = xmlDoc.CreateNavigator(); //evaluates XPath expressions
XPathNodeIterator node = xmlNav.Select("/DATA2SC/CALL");
string dne = xmlNav.GetAttribute("NTE", "");
Console.WriteLine(dne);
sr.Close();
XML
<?xml version="1.0"?>
<DATA2SC PIN="00000">
<CALL
TR_NUM="00000001"
STATUS="WAITING_FOR_APPROVAL"
NTE="$15.00">
<PROBLEM>
Text
</PROBLEM>
</CALL>
</DATA2SC>
Can you try this code please ?
I already check and it's work
StreamReader sr = new StreamReader("c:\\x.xml");
XPathDocument xmlDoc = new XPathDocument(sr); // holds xml document
XPathNavigator xmlNav = xmlDoc.CreateNavigator(); //evaluates XPath expressions
var node = xmlNav.SelectSingleNode("/DATA2SC/CALL");
string dne = node.GetAttribute("NTE", "");
Console.WriteLine(dne);
OR
XDocument docXmlWorld = XDocument.Load("c:\\x.xml");
foreach (var node1 in docXmlWorld.Descendants("DATA2SC"))
{
foreach (var node2 in node1.Descendants("CALL"))
{
string dne = node2.Attribute("NTE").Value;
Console.Out.WriteLine(dne);
}
}
Or you can do like this too:
XDocument docXmlWorld = XDocument.Load("c:\\x.xml");
//Get the first child => [DATA2SC]
XElement elementNodeDATA2SC = docXmlWorld.Element("DATA2SC");
//Get the first child => [CALL]
XElement elementNodeCALL = elementNodeDATA2SC.Element("CALL");
//Get the attribute NTE from [CALL] node
string dne = elementNodeCALL.Attribute("NTE").Value;
Console.Out.WriteLine(dne);
The Select method, returns a collection of all nodes with the specified XPath.
You can use SelectSingleNode, to select the first node.
var node = xmlNav.SelectSingleNode("/DATA2SC/CALL");
string dne = node.GetAttribute("NTE", "");

Create xml rootNode via c#

I want to create a xml document and root element like this:
<rdf:RDF xmlns:cim="http://iec.ch/TC57/2009/CIM-schema-cim14#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
I try create this like that:
XmlDocument doc = new XmlDocument();
XmlNode rootNode = doc.CreateElement("rdf:RDF xmlns:cim="http://iec.ch/TC57/2009/CIM-schema-cim14#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">");
doc.AppendChild(rootNode);
XmlNode userNode = doc.CreateElement("user");
XmlAttribute attribute = doc.CreateAttribute("age");
attribute.Value = "42";
userNode.Attributes.Append(attribute);
userNode.InnerText = "John Doe";
rootNode.AppendChild(userNode);
userNode = doc.CreateElement("user");
attribute = doc.CreateAttribute("age");
attribute.Value = "39";
userNode.Attributes.Append(attribute);
userNode.InnerText = "Jane Doe";
rootNode.AppendChild(userNode);
doc.Save("C:/xml-test.xml");
But i have exeption :The ' ' character, hexadecimal value 0x20, cannot be included in a name. Or so on.
How to make this element?
Thanks.
The method you're using for building XML is actually building a tree of objects (rather than as the textual representation of them), for for Schemas, you have to tell the document about them:
XmlDocument doc = new XmlDocument();
XmlSchemaSet xss = new XmlSchemaSet();
xss.Add("cim", "http://iec.ch/TC57/2009/CIM-schema-cim14#");
xss.Add("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
doc.Schemas = xss;
XmlNode rootNode = doc.CreateElement("rdf:RDF"); // This overload assumes the document already knows about the rdf schema as it is in the Schemas set
doc.AppendChild(rootNode);
If you can consider using Linq to XML, here's an alternative.
// Your data
var users = new List<User> {
new User { Name = "John", Age = 42 },
new User { Name = "Jane", Age = 39 }
};
// Project the data into XElements
var userElements =
from u in users
select
new XElement("user", u.Name,
new XAttribute("age", u.Age));
// Build the XML document, add namespaces and add the projected elements
var doc = new XDocument(
new XElement("RDF",
new XAttribute(XNamespace.Xmlns + "cim",
XNamespace.Get("http://iec.ch/TC57/2009/CIM-schema-cim14#")),
new XAttribute(XNamespace.Xmlns + "rdf",
XNamespace.Get("http://www.w3.org/1999/02/22-rdf-syntax-ns#")),
userElements
)
);
doc.Save(#"c:\xml-test.xml");

Categories