I know MusicBrainz has a version 2 available but v1 is the only way I got this far.
(This is 1 return, it is usually hundreds...):
XML:
<metadata xmlns="http://musicbrainz.org/ns/mmd-1.0#" xmlns:ext="http://musicbrainz.org/ns/ext-1.0#">
<release-list count="928" offset="0">
<release id="bea5602c-53bc-4416-af49-238aae51e8ea" type="Live Bootleg" ext:score="100">
<title>Seattle 1988</title>
<text-representation language="ENG" script="Latn" />
<artist id="5b11f4ce-a62d-471e-81fc-a69a8278c7da">
<name>Nirvana</name>
<sort-name>Nirvana</sort-name>
</artist>
<release-event-list>
<event format="CD" />
</release-event-list>
<disc-list count="0" />
<track-list count="10" />
</release>
</release-list>
</metadata>
I am able to get all albums returned with this :
client = new WebClient();
client.OpenReadCompleted += client_OpenReadCompleted;
// Call public web service.
string requestUri =
"http://musicbrainz.org/ws/1/release/?limit=100&type=xml&artist={0}";
client.OpenReadAsync(
new Uri(String.Format(
requestUri, "Jimi Hendrix")));
private void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
// Process returned data.
XElement results;
albums = new List<string>();
if (e.Error != null)
{ return; }
else
{
XNamespace ns =
#"http://musicbrainz.org/ns/mmd-1.0#";
results = XElement.Load(e.Result);
var q = from r in results.Descendants(ns + "release")
select new { Title = r.Element(ns + "title").Value };
foreach (var i in q)
{
albums.Add(i.Title);
Console.WriteLine(i.Title);
}
}
How can I also get the release id for each album?
As you already selected the release-node, you just need to get that id-attribute:
var q = from r in results.Descendants(ns + "release")
select new
{
Title = r.Element(ns + "title").Value,
Release = r.Attribute("id").Value
};
Here the class you can use to store your data
class Release
{
public Guid Id {get; set;}
public string Title {get; set;}
}
This is how you can retrieve the collection of Release from your respond.
XNamespace ns = "http://musicbrainz.org/ns/mmd-1.0#";
var doc = XElement.Load(e.Result);
IEnumerable<Release> res = doc.Descendants(ns + "release")
.Select(r => new Release{
Id = (Guid)r.Attribute("id"),
Title = (string)r.Element(ns + "title")
});
var q = from r in results.Descendants(ns + "release")
select new
{
Title = r.Element(ns + "title").Value,
Release = r.Attribute("id").Value
};
Works great! Thanks
Related
I am facing problem in fetching xml element value if it is having name space. please help me what is the problem here. My xml string is below
<PurchaseOrder xmlns:aw="http://www.adventure-works.com"> <aw:ShippingAddress> <aw:Name>John</aw:Name> <aw:Street>123 Main St.</aw:Street> <aw:City>Seattle</aw:City> <aw:State>WA</aw:State> <aw:Zip>98113</aw:Zip> <aw:Country>USA</aw:Country> </aw:ShippingAddress> <aw:ShippingAddress> <aw:Name>Chris Preston</aw:Name> <aw:Street>123 Robin St.</aw:Street> <aw:City>Newyork</aw:City> <aw:State>TU</aw:State> <aw:Zip>98113</aw:Zip> <aw:Country>USA</aw:Country> </aw:ShippingAddress> <aw:ShippingAddress> <aw:Name>Charlis</aw:Name> <aw:Street>53 Jacob St.</aw:Street> <aw:City>California</aw:City> <aw:State>DOWNTOWN</aw:State> <aw:Zip>98111</aw:Zip> <aw:Country>USA</aw:Country> </aw:ShippingAddress> </aw:PurchaseOrder>
my code is below
XDocument doc = XDocument.Load("PurchaseOrder.xml");
List<PurchaseOrder> listWO = new List<PurchaseOrder>();
foreach (XElement el in doc.Root.Elements())
{
if ( el.Elements().Count() > 0)
{
PurchaseOrder po = new PurchaseOrder
{
Name = el.Elements("aw:Name").First().Value,
City = el.Elements("aw:City").First().Value,
Country = el.Elements("aw:Country").First().Value
};
listPO.Add(po):
}
}
Here i am not getting the value of each "ShippingAddress" wise.
See changes below :
XDocument doc = XDocument.Load("PurchaseOrder.xml");
XNamespace awNs = doc.Root.GetNamespaceOfPrefix("aw");
List<PurchaseOrder> listWO = new List<PurchaseOrder>();
foreach (XElement el in doc.Root.Elements())
{
if ( el.Elements().Count() > 0)
{
PurchaseOrder po = new PurchaseOrder
{
Name = el.Elements(awNs + "Name").First().Value,
City = el.Elements(awNs + "City").First().Value,
Country = el.Elements(awNs + "Country").First().Value
};
listPO.Add(po):
}
}
I need to bind the search result from NEST (ElasticSearch) to a Gridview in ASP.NET (Webform).
Code I get the result from ElasticSearch from using NEST:
public class Address
{
public int SN { get; set; }
public string JLN { get; set; }
}
protected void BtnSearch_Clicked(object sender, EventArgs e)
{
string SearchValue = txtSearchValue.Text;
string es_host = System.Configuration.ConfigurationManager.AppSettings["cnStringIP"];
string es_port = System.Configuration.ConfigurationManager.AppSettings["cnStringPort"];
string es_index = System.Configuration.ConfigurationManager.AppSettings["cnStringIndex"];
var settings = new ConnectionSettings(new Uri("http://" + es_host + ":" + es_port + ""))
.DefaultIndex("masterlist*");
var client = new ElasticClient(settings);
var searchResponse = client.Search<Address>(s => s
.Index("masterlist*")
.From(0)
.Size(10)
.Query(q => q
.QueryString(qs => qs
.Query("JLN:\""+ SearchValue +"\"")
)
)
);
var address = searchResponse.Documents.ToList();
ESGridview.DataSource = address;
ESGridview.DataBind();
}
With this code, the gridview can auto-generate two fields of correct header which is "SN" and "JLN", and it can auto generate 10 rows (I limit the size to 10 rows max in search syntax) but it's empty data in the column.
I did found another POST with this link
https://www.elastic.co/guide/en/elasticsearch/client/net-api/6.x/returned-fields.html#returned-fields
After check with this link,
I changed my code to:
string SearchValue = txtSearchValue.Text;
string es_host = System.Configuration.ConfigurationManager.AppSettings["cnStringIP"];
string es_port = System.Configuration.ConfigurationManager.AppSettings["cnStringPort"];
string es_index = System.Configuration.ConfigurationManager.AppSettings["cnStringIndex"];
var settings = new ConnectionSettings(new Uri("http://" + es_host + ":" + es_port + ""))
.DefaultIndex("masterlist*");
var client = new ElasticClient(settings);
var searchResponse = client.Search<Address>(s => s
.StoredFields(sf => sf
.Fields(
f => f.SN,
f => f.JLN
)
)
.From(0)
.Size(10)
.Query(q => q
.QueryString(qs => qs
.Query("JLN:\""+ SearchValue +"\"")
)
)
);
foreach (var fieldValues in searchResponse.Fields)
{
var document = new
{
SN = fieldValues.ValueOf<Address, int>(p => p.SN),
JLN = fieldValues.Values<Address, string>(p => p.JLN)
};
}
var address = searchResponse.Documents;
var count = "MaxScore" + searchResponse.MaxScore;
ESGridview.DataSource = address;
ESGridview.DataBind();
But I get an error while run the code from start on whole foreach (var...) area :
System.NullReferenceException:'Object reference not set to an instance of an object.'
Did anyone can teach me how can solve this problem or anything I do fault ?
Many many thanks ~
ElasticSearch 7.0.1
NEST 7.0.0
C#
ASP.NET (Webform)
I solve my problem already.
The code below is how to get the searchResult from ElasticSearch and bind the data into Gridview in ASP.NET by using NEST.
public class Address
{
[Text(Name = "SN")]
public string SN { get; set; }
[Text(Name = "JLN")]
public string JLN { get; set; }
}
protected void BtnSearch_Clicked(object sender, EventArgs e)
{
string SearchValue = txtSearchValue.Text;
string es_host = System.Configuration.ConfigurationManager.AppSettings["cnStringIP"];
string es_port = System.Configuration.ConfigurationManager.AppSettings["cnStringPort"];
string es_index = System.Configuration.ConfigurationManager.AppSettings["cnStringIndex"];
var settings = new ConnectionSettings(new Uri("http://" + es_host + ":" + es_port + ""))
.DefaultIndex("masterlist*");
var client = new ElasticClient(settings);
var searchResponse = client.Search<Address>(s => s
.From(0)
.Size(100)
.Query(q => q
.QueryString(qs => qs
.Query("JLN:\"" + SearchValue + "\"")
)
)
);
var address = searchResponse.Documents.ToList();
ESGridview.DataSource = address;
ESGridview.DataBind();
}
how to load xml into collection and view in datagrid.
My Xml file
Add new record in xml
private void addToXml(bool value)
{
List<klientprywatny> klienci = new List<klientprywatny>();
klienci.Add(new klientprywatny() { Imie = txtImie.Text, Nazwisko = txtNazwisko.Text, miasto = txtMiasto.Text, ulica = txtUlica.Text,
kodpocztowy = txtKodPocztowy.Text, telefon = txtTelefon.Text, email = txtEmail.Text, numer = txtNumer.Text });
XDocument doc = XDocument.Load("Osoby.xml");
var osoba =
from klient in klienci
orderby klient.Nazwisko, klient.Imie
select new XElement("osoba",
new XElement("imie", klient.Imie),
new XElement("nazwisko", klient.Nazwisko),
new XElement("miasto", klient.miasto),
new XElement("ulica", klient.ulica),
new XElement("kodpocztowy", klient.kodpocztowy),
new XElement("telefon", klient.telefon),
new XElement("email", klient.email),
new XElement("numer", klient.numer)
);
doc.Root.Element("prywatni").Add(osoba);
doc.Save("Osoby.xml");
}
I tried
XDocument xml = XDocument.Load("Osoby.xml");
List<klientprywatny> klienci = (
from osoba in xml.Root.Elements("osoba")
select new osoba(
osoba.Element("imie").Value,
osoba.Element("nazwisko").Value,
osoba.Element("miasto").Value,
osoba.Element("ulica").Value,
osoba.Element("kodpocztowy").Value,
osoba.Element("telefon").Value,
osoba.Element("email").Value,
osoba.Element("numer").Value
)
).ToList<klientprywatny>();
but it does not work
You can get the elements from XML to your list as follows:
IEnumerable<osoba> result = from c in xml.Descendants("osoba")
select new osoba()
{
Imie = (string)c.Element("imie").Value,
Nazwisko = (string)c.Element("nazwisko").Value,
miasto = (string)c.Element("miasto").Value,
ulica = (string)c.Element("ulicia").Value,
kodpocztowy = (string)c.Element("kodpocztowy").Value,
email = (string)c.Element("telefon").Value,
numer = (string)c.Element("email").Value,
telefon = (string)c.Element("numer").Value,
};
I've tested this code, it works perfectly.
Hope it helps.
I am using c# and LINQ in a script transformation in VS2012 to parse a complex xml soap message. I cannot figure out how to loop through the xml and get all the elements that I want in a single row. The xml is below. This is only a portion of what is returned. There are 48 intervals for each item and generally 5-6 items.
<return>
<item>
<interval>
<intervalDate>
<day>8</day>
<month>7</month>
<year>2016</year>
</intervalDate>
<intervalTime>
<hours>0</hours>
<militaryTime>true</militaryTime>
<minutes>0</minutes>
<seconds>0</seconds>
</intervalTime>
<laborType>forecasted</laborType>
<volume>0.0</volume>
</interval>
<interval>
<intervalDate>
<day>8</day>
<month>7</month>
<year>2016</year>
</intervalDate>
<intervalTime>
<hours>0</hours>
<militaryTime>true</militaryTime>
<minutes>30</minutes>
<seconds>0</seconds>
</intervalTime>
<laborType>forecasted</laborType>
<volume>0.0</volume>
</interval>
<jobCode>1</jobCode>
<jobName>SERVER</jobName>
</item>
<item>
<interval>
<intervalDate>
<day>8</day>
<month>7</month>
<year>2016</year>
</intervalDate>
<intervalTime>
<hours>0</hours>
<militaryTime>true</militaryTime>
<minutes>0</minutes>
<seconds>0</seconds>
</intervalTime>
<laborType>forecasted</laborType>
<volume>0.0</volume>
</interval>
<interval>
<intervalDate>
<day>8</day>
<month>7</month>
<year>2016</year>
</intervalDate>
<intervalTime>
<hours>0</hours>
<militaryTime>true</militaryTime>
<minutes>30</minutes>
<seconds>0</seconds>
</intervalTime>
<laborType>forecasted</laborType>
<volume>0.0</volume>
</interval>
<jobCode>50</jobCode>
<jobName>Cashier</jobName>
</item>
I need to output the data into 5 columns, datetime, laborType, laborVolume, laborJobCode, and laborJobName.
laborDateTime|laborType|laborVolume|laborJobCode|laborJobName
2016-08-07 00:00:00.000|forecasted|0|1|SERVER
2016-08-07 00:30:00.000|forecasted|0|1|SERVER
2016-08-07 01:00:00.000|forecasted|0|1|SERVER
2016-08-07 01:30:00.000|forecasted|0|1|SERVER
I have not been able to find any examples online that show how to do this. If I loop through Interval, I return the expected number of rows but have no way of getting the JobCode and JobName.
public override void CreateNewOutputRows()
{
/*
Add rows by calling the AddRow method on the member variable named "<Output Name>Buffer".
For example, call MyOutputBuffer.AddRow() if your output was named "MyOutput".
*/
String content = Variables.XMLString;
XElement xdoc = XElement.Parse(content);
var Interval = from interval in xdoc.Descendants("interval")
select new
{
laborIntervalDay = interval.Element("intervalDate").Element("day").Value,
laborIntervalMonth = interval.Element("intervalDate").Element("month").Value,
laborIntervalYear = interval.Element("intervalDate").Element("year").Value,
laborIntervalHour = interval.Element("intervalTime").Element("hours").Value,
laborIntervalMinutes = interval.Element("intervalTime").Element("minutes").Value,
laborIntervalSeconds = interval.Element("intervalTime").Element("seconds").Value,
laborIntervalMilitary = interval.Element("intervalTime").Element("militaryTime").Value,
laborType = interval.Element("laborType").Value,
laborVolume = interval.Element("volume").Value
};
foreach (var q in Interval)
{
try
{
DateTime dtBusinessDate = new DateTime(Convert.ToInt32(q.laborIntervalYear), Convert.ToInt32(q.laborIntervalMonth), Convert.ToInt32(q.laborIntervalDay), Convert.ToInt32(q.laborIntervalHour), Convert.ToInt32(q.laborIntervalMinutes), Convert.ToInt32(q.laborIntervalSeconds));
OUTLaborBuffer.AddRow();
OUTLaborBuffer.laborDateTime = dtBusinessDate;
OUTLaborBuffer.laborType = Convert.ToString(q.laborType);
OUTLaborBuffer.laborVolume = Convert.ToDouble(q.laborVolume);
//OUTLaborBuffer.laborJobCode = Convert.ToInt64(p.laborJobCode);
//OUTLaborBuffer.laborJobName = p.laborJobName;
}
catch (Exception ex)
{
MessageBox.Show("Error Message: " + ex.Message + " Message Detail: " + ex.StackTrace);
}
}
}
I've also tried looping through Labor and Interval but this is not correct because it is assigning the JobCode/JobName of the current iteration of Labor to all intervals. If there are 5 items then I end up with 5x the expected rows.
var Interval = from interval in xdoc.Descendants("interval")
select new
{
laborIntervalDay = interval.Element("intervalDate").Element("day").Value,
laborIntervalMonth = interval.Element("intervalDate").Element("month").Value,
laborIntervalYear = interval.Element("intervalDate").Element("year").Value,
laborIntervalHour = interval.Element("intervalTime").Element("hours").Value,
laborIntervalMinutes = interval.Element("intervalTime").Element("minutes").Value,
laborIntervalSeconds = interval.Element("intervalTime").Element("seconds").Value,
laborIntervalMilitary = interval.Element("intervalTime").Element("militaryTime").Value,
laborType = interval.Element("laborType").Value,
laborVolume = interval.Element("volume").Value
};
var Labor = from item in xdoc.Descendants("item")
select new
{
laborJobCode = item.Element("jobCode").Value,
laborJobName = item.Element("jobName").Value,
};
foreach (var p in Labor)
{
// Save check information
try
{
foreach (var q in Interval)
{
try
{
DateTime dtBusinessDate = new DateTime(Convert.ToInt32(q.laborIntervalYear), Convert.ToInt32(q.laborIntervalMonth), Convert.ToInt32(q.laborIntervalDay), Convert.ToInt32(q.laborIntervalHour), Convert.ToInt32(q.laborIntervalMinutes), Convert.ToInt32(q.laborIntervalSeconds));
OUTLaborBuffer.AddRow();
OUTLaborBuffer.laborDateTime = dtBusinessDate;
OUTLaborBuffer.laborType = Convert.ToString(q.laborType);
OUTLaborBuffer.laborVolume = Convert.ToDouble(q.laborVolume);
OUTLaborBuffer.laborJobCode = Convert.ToInt64(p.laborJobCode);
OUTLaborBuffer.laborJobName = p.laborJobName;
}
catch (Exception ex)
{
MessageBox.Show("Error Message: " + ex.Message + " Message Detail: " + ex.StackTrace);
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error Message: " + ex.Message + " Message Detail: " + ex.StackTrace);
}
}
}
I have found some examples online showing of complex xml being parsed like below:
var Labor = from item in xdoc.Descendants("item")
select new
{
laborJobCode = item.Element("jobCode").Value,
laborJobName = item.Element("jobName").Value,
laborInterval = (from interval in xdoc.Descendants("interval")
select new
{
laborIntervalDay = interval.Element("intervalDate").Element("day").Value,
laborIntervalMonth = interval.Element("intervalDate").Element("month").Value,
laborIntervalYear = interval.Element("intervalDate").Element("year").Value,
laborIntervalHour = interval.Element("intervalTime").Element("hours").Value,
laborIntervalMinutes = interval.Element("intervalTime").Element("minutes").Value,
laborIntervalSeconds = interval.Element("intervalTime").Element("seconds").Value,
laborIntervalMilitary = interval.Element("intervalTime").Element("militaryTime").Value,
laborType = interval.Element("laborType").Value,
laborVolume = interval.Element("volume").Value
})
};
The examples I've found with this structure are outputting ToList and I can't figure out how to output the descendants of interval in this format. Do you have any idea how this can be done?
Something like this should do the trick:
public dynamic OUTLaborBuffer;
public dynamic Variables;
public void CreateNewOutputRows()
{
String content = Variables.XMLString;
XElement data = XElement.Parse(content);
foreach (var item in data.XPathSelectElements("//item"))
{
var jobCode = item.Element("jobCode").Value;
var jobName = item.Element("jobName").Value;
foreach (var interval in item.XPathSelectElements("//interval"))
{
var laborIntervalDay = interval.Element("intervalDate").Element("day").Value;
var laborIntervalMonth = interval.Element("intervalDate").Element("month").Value;
var laborIntervalYear = interval.Element("intervalDate").Element("year").Value;
var laborIntervalHour = interval.Element("intervalTime").Element("hours").Value;
var laborIntervalMinutes = interval.Element("intervalTime").Element("minutes").Value;
var laborIntervalSeconds = interval.Element("intervalTime").Element("seconds").Value;
var laborIntervalMilitary = interval.Element("intervalTime").Element("militaryTime").Value;
var laborType = interval.Element("laborType").Value;
var laborVolume = interval.Element("volume").Value;
DateTime dtBusinessDate = new DateTime(Convert.ToInt32(laborIntervalYear), Convert.ToInt32(laborIntervalMonth), Convert.ToInt32(q.laborIntervalDay), Convert.ToInt32(laborIntervalHour), Convert.ToInt32(laborIntervalMinutes), Convert.ToInt32(laborIntervalSeconds));
OUTLaborBuffer.AddRow();
OUTLaborBuffer.laborDateTime = dtBusinessDate;
OUTLaborBuffer.laborType = Convert.ToString(laborType);
OUTLaborBuffer.laborVolume = Convert.ToDouble(laborVolume);
OUTLaborBuffer.laborJobCode = Convert.ToInt64(jobCode);
OUTLaborBuffer.laborJobName = jobName;
}
}
}
feel free to change XPathSelectElements back to Descendants
I was able to solve this by using parent to get the JobCode and JobName while looping through intervals. I based it off of on this example: Linq on Complex XML
Here is the code I ended up with:
public override void CreateNewOutputRows()
{
/*
Add rows by calling the AddRow method on the member variable named "<Output Name>Buffer".
For example, call MyOutputBuffer.AddRow() if your output was named "MyOutput".
*/
String content = Variables.XMLString;
XElement xdoc = XElement.Parse(content);
var Interval = from interval in xdoc.Descendants("interval")
select new
{
laborJobCode = interval.Parent.Element("jobCode").Value,
laborJobName = interval.Parent.Element("jobName").Value,
laborIntervalDay = interval.Element("intervalDate").Element("day").Value,
laborIntervalMonth = interval.Element("intervalDate").Element("month").Value,
laborIntervalYear = interval.Element("intervalDate").Element("year").Value,
laborIntervalHour = interval.Element("intervalTime").Element("hours").Value,
laborIntervalMinutes = interval.Element("intervalTime").Element("minutes").Value,
laborIntervalSeconds = interval.Element("intervalTime").Element("seconds").Value,
laborIntervalMilitary = interval.Element("intervalTime").Element("militaryTime").Value,
laborType = interval.Element("laborType").Value,
laborVolume = interval.Element("volume").Value
};
foreach (var q in Interval)
{
try
{
DateTime dtBusinessDate = new DateTime(Convert.ToInt32(q.laborIntervalYear), Convert.ToInt32(q.laborIntervalMonth), Convert.ToInt32(q.laborIntervalDay), Convert.ToInt32(q.laborIntervalHour), Convert.ToInt32(q.laborIntervalMinutes), Convert.ToInt32(q.laborIntervalSeconds));
OUTLaborBuffer.AddRow();
OUTLaborBuffer.laborDateTime = dtBusinessDate;
OUTLaborBuffer.laborType = Convert.ToString(q.laborType);
OUTLaborBuffer.laborVolume = Convert.ToDouble(q.laborVolume);
OUTLaborBuffer.laborJobCode = Convert.ToInt64(q.laborJobCode);
OUTLaborBuffer.laborJobName = Convert.ToString(q.laborJobName);
}
catch (Exception ex)
{
MessageBox.Show("Error Message: " + ex.Message + " Message Detail: " + ex.StackTrace);
}
}
}
I have a problem, I am trying to list all my tfs projects and I am having this exception:
Value does not fall within the expected range.
Here is my method
public async Task<XElement> Deserialize()
{
string xml = "https://tfsodata.visualstudio.com/DefaultCollection/Projects('xxxx')/WorkItems";
var feed = await this.GetAsync(PROJECTS_PATH);
var x = feed.Items.ToString();
XElement dataTfs = new XElement(x);
foreach(var tfsdata in feed.Items)
{
var xDoc = XDocument.Parse(tfsdata.Content.Text);
IEnumerable<XElement> elem = xDoc.Elements();
XNamespace m = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";
XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices";
var data = from query in xDoc.Descendants(m + "properties")
select new TfsEntities.Models.TfsEntities.properties
{
Title = (string)query.Element(d + "Title"),
State = (string)query.Element(d + "State"),
Reason = (string)query.Element(d + "Reason")
};
dataTfs.Add(data.ToList());
}
return (XElement)dataTfs;
}
Does anyone know how to fix this problem please?
Thank you