XML read and getting values - c#

How can I get a this: val="icon.weapon_small_sword_i00"/> from XML file by giving a itemId. I managed to do something like this but besides of choosing right itemId from file I dont know how to get the value I mentioned above.
Thats a code I have:
int ItemId = 15;
XmlTextReader reader = new XmlTextReader(#"D:\L2Eq\xml\items");
XmlNodeType type;
while (reader.Read())
{
type = reader.NodeType;
if (type == XmlNodeType.Element)
{
if (reader.Name == "item")
{
if (Int32.Parse(reader.GetAttribute(0)) == ItemId)
{
Console.WriteLine(reader.GetAttribute(0));
}
}
}
}
And thats how XML file looks like:
<list xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="items.xsd">
<item id="1" type="Weapon" name="Short Sword">
<set name="icon" val="icon.weapon_small_sword_i00" />
<set name="default_action" val="equip" />
<set name="weapon_type" val="sword" />
<set name="bodypart" val="rhand" />
<set name="random_damage" val="10" />
<set name="attack_range" val="40" />
<for>
<set order="0x08" stat="pAtk" val="8" />
<set order="0x08" stat="mAtk" val="6" />
<set order="0x08" stat="rCrit" val="8" />
<set order="0x08" stat="pAtkSpd" val="379" />
</for>
</item>
<item id="2" type="Weapon" name="Long Sword">
<set name="icon" val="icon.weapon_long_sword_i00" />
<set name="default_action" val="equip" />
<set name="weapon_type" val="sword" />
<set name="bodypart" val="rhand" />
<set name="random_damage" val="10" />
<set name="attack_range" val="40" />
<for>
<set order="0x08" stat="pAtk" val="24" />
<set order="0x08" stat="mAtk" val="17" />
<set order="0x08" stat="rCrit" val="8" />
<set order="0x08" stat="pAtkSpd" val="379" />
</for>
</item>
</list>

XmlTextReader is a very clumsy way to deal with XML; I'd never use it. The old System.Xml.XmlDocument API is a much better choice:
var findID = "1";
string iconValue = null;
var xdoc = new System.Xml.XmlDocument();
xdoc.Load(#"D:\L2Eq\xml\items");
iconValue = xdoc.SelectSingleNode("/list/item[#id=" + findID + "]/set[#name='icon']/#val")?.Value;
Or you could use the shiny new LINQ to XML classes:
var doc = XDocument.Load(#"D:\L2Eq\xml\items");
iconValue = doc.Descendants("item")
.Where(d => d.Attribute("id")?.Value == findID)
.Descendants("set")
.Where(x => x.Attribute("name")?.Value == "icon")
.FirstOrDefault()?.Attribute("val")?.Value;

Related

Export Fusionchart to PDF using iTextSharp

I would like to export my FusionChart to PDF using iTextSharp. I am able to export to PDF by use of setting the charts exportEnabled = '1', but I want to be able to export the fusionchart from my controller.
I have my entire fusionchart in a str variable and tried the following :
public FileStreamResult ExportPDF(int ProjectID)
{
string exportData = String.Format("<html><body>{0}</body></html>", getGraphXMLData(ProjectID));//This Gets the chart and stores into string
var bytes = System.Text.Encoding.UTF8.GetBytes(exportData);
using (var input = new MemoryStream(bytes))
{
var output = new MemoryStream();
var document = new iTextSharp.text.Document(PageSize.A4, 50, 50, 50, 50);
var writer = PdfWriter.GetInstance(document, output);
Font headerFont = FontFactory.GetFont("Verdana", 10);
Font rowfont = FontFactory.GetFont("Verdana", 10);
writer.CloseStream = false;
document.Open();
var xmlWorker = iTextSharp.tool.xml.XMLWorkerHelper.GetInstance();
xmlWorker.ParseXHtml(writer, document, input, System.Text.Encoding.UTF8);
document.Close();
output.Position = 0;
return File(output, "application/pdf", "ProfileChart_Report.pdf");
//return new FileStreamResult(output, "application/pdf");
}
}
I however get an error on this line : document.Close();
The error im getting is :
An exception of type 'System.IO.IOException' occurred in iTextSharp.dll but was not handled in user code
Additional information: The document has no pages.
Below is the data in the exportData field:
<html>
<body>
<chart caption='Project Test' yaxisname='Elevation' xaxisname='Accumulated Length' xaxismaxvalue='1000' xaxisminvalue='100' yaxismaxvalue='200' xnumberprefix='' ynumberprefix='' showcanvasborder='1' canvasborderthickness='0.5' canvasborderalpha= '50' showXAxisLine= '0' showformbtn= '1' formAction='#' submitdataasxml='1' baseFontColor='#333333' baseFont='Helvetica Neue, Arial' captionFontSize='14' subcaptionFontSize='14' subcaptionFontBold='0' showBorder='0' bgColor='#ffffff' showValues='0' showShadow='0' canvasBgColor='#ffffff' divlineAlpha='100' divlineColor='#999999' divlineThickness='1' divLineIsDashed='1' divLineDashLen='1' divLineGapLen='1' usePlotGradientColor='0' showplotborder='0' showAlternateHGridColor='0' showAlternateVGridColor='0' legendBgAlpha='0' legendBorderAlpha='0' legendShadow='0' legendItemFontSize='10' legendItemFontColor='#666666' toolTipColor='#ffffff' toolTipBorderThickness='0' toolTipBgColor='#000000' toolTipBgAlpha='80' toolTipBorderRadius='2' toolTipPadding = '5' labeldisplay= 'rotate' slantlabels= '1' exportEnabled='1' > <categories verticallinecolor='666666' verticallinethickness='1' alpha='40' anchorsides='0' anchorradius='0'><category label='0' x='0' showverticalline='0'/>
<category label='46.17' x='46.17' showverticalline='0'/>
<category label='142.98' x='142.98' showverticalline='0'/>
<category label='543.88' x='543.88' showverticalline='0'/>
<category label='826.58' x='826.58' showverticalline='0'/>
<category label='1037.93' x='1037.93' showverticalline='0'/>
<category label='1106.1' x='1106.1' showverticalline='0'/>
<category label='1338.1' x='1338.1' showverticalline='0'/>
<category label='1838.5' x='1838.5' showverticalline='0'/>
<category label='1889.37' x='1889.37' showverticalline='0'/>
<category label='2538' x='2538' showverticalline='0'/>
<category label='2789.04' x='2789.04' showverticalline='0'/>
<category label='3154.34' x='3154.34' showverticalline='0'/>
<category label='3345.17' x='3345.17' showverticalline='0'/>
<category label='4220.75' x='4220.75' showverticalline='0'/>
<category label='4671.9' x='4671.9' showverticalline='0'/>
<category label='5057.74' x='5057.74' showverticalline='0'/>
<category label='5318.51' x='5318.51' showverticalline='0'/>
<category label='5576.18' x='5576.18' showverticalline='0'/>
<category label='5958.13' x='5958.13' showverticalline='0'/>
<category label='6537.92' x='6537.92' showverticalline='0'/>
<category label='7282.09' x='7282.09' showverticalline='0'/>
<category label='8178.84' x='8178.84' showverticalline='0'/>
<category label='8511.22' x='8511.22' showverticalline='0'/>
<category label='8989.59' x='8989.59' showverticalline='0'/>
<category label='9442.78' x='9442.78' showverticalline='0'/>
<category label='9862.78' x='9862.78' showverticalline='0'/>
<category label='9942.78' x='9942.78' showverticalline='0'/>
<category label='10402.78' x='10402.78' showverticalline='0'/>
<category label='10607.78' x='10607.78' showverticalline='0'/>
<category label='11052.78' x='11052.78' showverticalline='0'/>
<category label='11267.78' x='11267.78' showverticalline='0'/>
<category label='11797.78' x='11797.78' showverticalline='0'/>
<category label='12307.78' x='12307.78' showverticalline='0'/>
<category label='12572.78' x='12572.78' showverticalline='0'/>
<category label='13229.78' x='13229.78' showverticalline='0'/>
<category label='13369.78' x='13369.78' showverticalline='0'/>
</categories>
<dataset drawline= '1' seriesname= 'Elevation' color= '#0045ff' anchorsides= '0' anchorradius= '0' anchorbgcolor= '#0045ff' anchorbordercolor= '#0045ff'>
<set y='321' x='0'/>
<set y='322' x='46.17'/>
<set y='322.54' x='142.98'/>
<set y='309.12' x='543.88'/>
<set y='292.63' x='826.58'/>
<set y='299.12' x='1037.93'/>
<set y='298.68' x='1106.1'/>
<set y='303.49' x='1338.1'/>
<set y='294.23' x='1838.5'/>
<set y='295.77' x='1889.37'/>
<set y='269.98' x='2538'/>
<set y='274.17' x='2789.04'/>
<set y='261.71' x='3154.34'/>
<set y='267.01' x='3345.17'/>
<set y='243.04' x='4220.75'/>
<set y='247.05' x='4671.9'/>
<set y='250.81' x='5057.74'/>
<set y='246.4' x='5318.51'/>
<set y='249.62' x='5576.18'/>
<set y='238.03' x='5958.13'/>
<set y='253.08' x='6537.92'/>
<set y='235.53' x='7282.09'/>
<set y='173.53' x='8178.84'/>
<set y='189.91' x='8511.22'/>
<set y='224.23' x='8989.59'/>
<set y='250' x='9442.78'/>
<set y='250' x='9862.78'/>
<set y='253.45' x='9942.78'/>
<set y='240' x='10402.78'/>
<set y='235' x='10607.78'/>
<set y='210' x='11052.78'/>
<set y='210' x='11267.78'/>
<set y='195' x='11797.78'/>
<set y='170' x='12307.78'/>
<set y='160' x='12572.78'/>
<set y='150' x='13229.78'/>
<set y='141.36' x='13369.78'/>
</dataset>
<dataset drawline= '1' seriesname= 'HGL' color= '#198500' anchorsides= '0' anchorradius= '0' anchorbgcolor= '#198500' anchorbordercolor= '#198500'>
<set y='321' x='0'/>
<set y='320.87' x='46.17'/>
<set y='320.59' x='142.98'/>
<set y='319.46' x='543.88'/>
<set y='318.66' x='826.58'/>
<set y='318.06' x='1037.93'/>
<set y='317.87' x='1106.1'/>
<set y='317.22' x='1338.1'/>
<set y='315.81' x='1838.5'/>
<set y='315.67' x='1889.37'/>
<set y='313.84' x='2538'/>
<set y='313.1' x='2789.04'/>
<set y='312.02' x='3154.34'/>
<set y='311.38' x='3345.17'/>
<set y='308.46' x='4220.75'/>
<set y='306.96' x='4671.9'/>
<set y='305.68' x='5057.74'/>
<set y='304.81' x='5318.51'/>
<set y='303.95' x='5576.18'/>
<set y='302.68' x='5958.13'/>
<set y='300.75' x='6537.92'/>
<set y='298.27' x='7282.09'/>
<set y='294.88' x='8178.84'/>
<set y='293.39' x='8511.22'/>
<set y='291.25' x='8989.59'/>
<set y='285.78' x='9442.78'/>
<set y='281.31' x='9862.78'/>
<set y='280.46' x='9942.78'/>
<set y='275.55' x='10402.78'/>
<set y='273.37' x='10607.78'/>
<set y='256.15' x='11052.78'/>
<set y='247.84' x='11267.78'/>
<set y='227.36' x='11797.78'/>
<set y='204.03' x='12307.78'/>
<set y='191.92' x='12572.78'/>
<set y='156.49' x='13229.78'/>
<set y='148.94' x='13369.78'/>
</dataset>
<dataset drawline= '0' seriesname= 'Peak' color= '#ff0000' anchorsides= '3' anchorradius= '5' anchorbgcolor= '#ff0000' anchorbordercolor= '#ff0000'>
<set y='321' x='0'/>
<set y='322.54' x='142.98'/>
<set y='299.12' x='1037.93'/>
<set y='303.49' x='1338.1'/>
<set y='295.77' x='1889.37'/>
<set y='274.17' x='2789.04'/>
<set y='267.01' x='3345.17'/>
<set y='250.81' x='5057.74'/>
<set y='249.62' x='5576.18'/>
<set y='253.08' x='6537.92'/>
<set y='253.45' x='9942.78'/>
<set y='141.36' x='13369.78'/>
</dataset>
<dataset drawline= '0' seriesname= 'Valve - Change' color= '#198500' anchorsides= '4' anchorradius= '5' anchorbgcolor= '#198500' anchorbordercolor= '#198500'>
<set y='309.12' x='543.88'/>
<set y='235.53' x='7282.09'/>
<set y='235' x='10607.78'/>
<set y='195' x='11797.78'/>
<set y='150' x='13229.78'/>
</dataset>
<dataset drawline= '0' seriesname= 'Valve suggest - Long Node' color= '#fdff00' anchorsides= '4' anchorradius= '3' anchorbgcolor= '#fdff00' anchorbordercolor= '#fdff00'>
<set y='321' x='0'/>
<set y='309.12' x='543.88'/>
<set y='294.23' x='1838.5'/>
<set y='269.98' x='2538'/>
<set y='261.71' x='3154.34'/>
<set y='243.04' x='4220.75'/>
<set y='247.05' x='4671.9'/>
<set y='250.81' x='5057.74'/>
<set y='238.03' x='5958.13'/>
<set y='253.08' x='6537.92'/>
<set y='235.53' x='7282.09'/>
<set y='173.53' x='8178.84'/>
<set y='189.91' x='8511.22'/>
<set y='224.23' x='8989.59'/>
<set y='250' x='9442.78'/>
<set y='250' x='9862.78'/>
<set y='240' x='10402.78'/>
<set y='210' x='11052.78'/>
<set y='195' x='11797.78'/>
<set y='170' x='12307.78'/>
<set y='150' x='13229.78'/>
</dataset>
</chart>
</body>
</html>
I specifically want to do it in iTextSharp as I have other exports that im doing that is also using iTextSharp and ultimately want to combine everything into one PDF.
I would appreciate anyones help. Thanks.

How to draw multi-series column chart in FusionCharts

I am developing a web application using asp.net c# and drawing graphs using FusionCharts. I need to draw a multi-series column chart for which i tries the following code, but in vain
<div id="chartContainer"></div>
<script type="text/javascript">
var myChart = new FusionCharts("Column3D", "myChartId", "400", "300");
myChart.setXMLUrl("Data8.xml");
myChart.render("chartContainer");
</script>
Data8.xml
----------
<chart caption='Sales by Product' numberPrefix='$' formatNumberScale='1' rotateValues='1' placeValuesInside='1' decimals='0' ><categories>
<category label='Product A' />
<category label='Product B' />
<category label='Product C' />
<category label='Product D' />
<category label='Product E' />
<category label='Product F' />
</categories><dataset seriesName='Current Year'>
<set value='567500' />
<set value='815300' />
<set value='556800' />
<set value='734500' />
<set value='676800' />
<set value='648500' />
</dataset><dataset seriesName='Previous Year'>
<set value='547300' />
<set value='584500' />
<set value='754000' />
<set value='456300' />
<set value='754500' />
<set value='437600' />
</dataset></chart>
Please help me to do this...
Try to modify:
var myChart = new FusionCharts("Column3D", "myChartId", "400", "300");
With:
var myChart = new FusionCharts("MSColumn3D", "myChartId", "400", "300");
Column3D is for a single serie, and MSColumn3D is for Multi-series.

XPathNavigator with xpath gives wrong node back

I try to get some specific values from an xml config. See example below.
<?xml version="1.0" encoding="utf-8"?>
<ExcelConfig>
<ExcelDocument name="Customer" type="flat">
<IdentityColumn>
<Column name="Id" />
</IdentityColumn>
<Validate>
<Column name="Name" mandatory="true" />
<Column name="FirstName" mandatory="true" />
<OrColumns mandatory="true">
<Column name="PostalCode" mandatory="false" />
<Column name="PostalCode2" mandatory="false" />
</OrColumns>
</Validate>
</ExcelDocument>
<ExcelDocument name="Company" type="flat">
<IdentityColumn>
<Column name="Id" />
</IdentityColumn>
<Validate>
<Column name="Name" mandatory="true" />
<Column name="FirstName" mandatory="true" />
<OrColumns mandatory="true">
<Column name="PostalCode" mandatory="false" />
<Column name="PostalCode2" mandatory="false" />
</OrColumns>
</Validate>
</ExcelDocument>
<ExcelDocument name="SomeOtherType" type="block">
<IdentityBlock>
<Column name="Period" col="A" />
<Column name="Period2" col="B" />
</IdentityBlock>
<Validate>
<Column name="Name" mandatory="true" />
<Column name="FirstName" mandatory="true" />
</Validate>
</ExcelDocument>
</ExcelConfig>
I use the following code to get some information from the excel file.
"ValidationConfiguration" is the string with the previous configuration.
//Get Different NodeTypes of Excel documents
List<XPathNavigator> types = XmlHelper.GetNodeTypes(validationConfiguration, "/ExcelConfig/ExcelDocument");
List<XPathNavigator> flatTypes = XmlHelper.GetNodeTypes(validationConfiguration,
"//ExcelConfig/ExcelDocument[#type='flat']");
List<XPathNavigator> blockTypes = XmlHelper.GetNodeTypes(validationConfiguration,
"//ExcelConfig/ExcelDocument[#type='block']");
//First we check if the file is from the flat type and get the IdentityColumns
List<XPathNavigator> identityColumnsNode = XmlHelper.GetNodeTypes(validationConfiguration, "//ExcelConfig/ExcelDocument[#type='flat']/IdentityColumn");
You can find the XmlHelper class below.
public static class XmlHelper
{
public static List<XPathNavigator> GetNodeTypes(string xmlConfiguration,string xPath)
{
XPathDocument doc = new XPathDocument(new StringReader(xmlConfiguration));
XPathNavigator nav = doc.CreateNavigator();
XPathExpression expr = nav.Compile(xPath);
List<XPathNavigator> elements = new List<XPathNavigator>();
foreach (XPathNavigator node in nav.Select(expr))
{
elements.Add(node);
}
return elements;
}
public static List<string> GetIdentityColumnNames(List<XPathNavigator> xPathNavigators)
{
List<string> identityColumns = new List<string>();
foreach (XPathNavigator xPathNavigator in xPathNavigators)
{
foreach (XPathNavigator test in xPathNavigator.Select("//Column"))
{
identityColumns.Add(test.GetAttribute("name", ""));
}
}
return identityColumns;
}
}
Now i want to do the following. I selected the identityColumnsNodes(they contains the IdentityColumn from the exceldocuments that have the flat type).
The i get for al that types the colums. But when i try that, i get all columns back from the whole file. He don't only the items from the node that i use.
foreach (XPathNavigator identityColumNode in identityColumnsNode)
{
List<string> identityColumns = XmlHelper.GetIdentityColumnNames(identityColumnsNode);
}
The second problem/thing i want to do --> the best way to select the right validate node from the specific file. With the identityColumns (that i get back and my list of HeaderRow Cells i know what file it is. But how can i select that validate node?
Or are their better methods to do this stuff?

C# Parsing XML and XElement

I have this code. Also here is a sample of the XML. I apologize for any confusion
<object type="node" >
<property name="id" value="1" />
<property name="name" value="ossvc06_node1" />
<property name="port_id" value="50050768014062AC" />
<property name="port_status" value="active" />
<property name="port_speed" value="4Gb" />
<property name="port_id" value="50050768013062AC" />
<property name="port_status" value="active" />
<property name="port_speed" value="4Gb" />
<property name="port_id" value="50050768011062AC" />
<property name="port_status" value="active" />
<property name="port_speed" value="4Gb" />
<property name="port_id" value="50050768012062AC" />
<property name="port_status" value="active" />
<property name="port_speed" value="4Gb" />
<property name="hardware" value="8G4" />
<property name="iscsi_name" value="iqn.1986-03.com.ibm:2145.ossvc06.ossvc06node1" />
<property name="iscsi_alias" value="" />
<property name="failover_active" value="no" />
<property name="failover_name" value="ossvc06_node2" />
<property name="failover_iscsi_name" value="iqn.1986- .com.ibm:2145.ossvc06.ossvc06node2" />
<property name="failover_iscsi_alias" value="" />
<property name="front_panel_id" value="115286" />
</object>
In the input file there are two of these objects of type "Node" each with different values for the property tags.
In this code I am looking for all the objects of type "node" in the incoming XML. There are 2 of them. The 'var nodes' statement evaluates correctly. In the debugger I can see two XElements with what appears to be the proper type and elements in the element list. However, the statement that gets the elements and assigns them to a list has ChildElements from both of the objects of type "node" that are in the XML and I am not sure why.
//load the input file
XDocument xdoc = XDocument.Load(_InputFile);
//get the object of type 'node'
//this code gives the results expected
// in the debugger each XElement appears to have the proper value and childElements
var nodes = from node in xdoc.Descendants("object")
where node.Attribute("type").Value == "node"
select node;
foreach (XElement nodelement in nodes)
{
// problem happens here, the child elements from both nodes get assigned to the list
List<XElement> nodeles = nodelement.Elements().ToList();
Node node = NodeFactory(nodelement);
// now assign the node to the correct IO group
var iogrp = SVCClusters[0].IOGroups.Where(io => io.Name == node.IOGroupName);
if (iogrp.FirstOrDefault().FirstNode == null) { iogrp.FirstOrDefault().FirstNode = node; }
else { iogrp.FirstOrDefault().SecondNode = node; }
}
Can you run this in a console app and tell me what you get
static void Main(string[] args)
{
XDocument xdoc = XDocument.Parse("<root><object type=\"node\" ><property name=\"id\" value=\"1\" /><property name=\"name\" value=\"ossvc06_node1\" /></object><object type=\"node\" ><property name=\"id\" value=\"2\" /><property name=\"name\" value=\"ossvc06_node2\" /></object></root>");
var nodes = xdoc.Descendants("object").Where(node => node.Attribute("type").Value == "node").ToList();
foreach (XElement nodelement in nodes)
{
List<XElement> nodeles = nodelement.Elements().ToList();
foreach (var node in nodeles)
Console.WriteLine(node);
}
}
You should have 4 rows output to the console
Edit---
Original issue was that an XPath expression "//property[#name='port_id']" was being used. This queries from the document root, not from the current node.
Change the XPath to be either "property[#name='port_id'] or ".//property[#name='port_id']"

select value of attribute by linq to xml

in my xml file how i can select the value for attributes TagId in ServiceAssignment elements by linq to xml
Note : this xml in a String Property not in xml file
<AnchoredXml xmlns="urn:schema:Microsoft.Rtc.Management.ScopeFramework.2008" SchemaWriteVersion="1">
<Key ScopeClass="Global">
<SchemaId Namespace="urn:schema:Microsoft.Rtc.Management.Settings.ServiceAssignment.2008" ElementName="ServiceAssignments" />
<AuthorityId Class="Host" InstanceId="00000000-0000-0000-0000-000000000000" />
</Key>
<Dictionary Count="1">
<Item>
<Key />
<Value Signature="2ffb6b0d-0239-4016-b08b-40520d1687ff">
<ServiceAssignments xmlns="urn:schema:Microsoft.Rtc.Management.Settings.ServiceAssignment.2008">
<ServiceAssignment TagId="659550892">
<Component Name="Registrar">
<ServiceId xmlns="urn:schema:Microsoft.Rtc.Management.Deploy.Topology.2008" SiteId="1" RoleName="Registrar" Instance="1" />
</Component>
<Component Name="PresenceFocus">
<ServiceId xmlns="urn:schema:Microsoft.Rtc.Management.Deploy.Topology.2008" SiteId="1" RoleName="UserServices" Instance="1" />
</Component>
</ServiceAssignment>
<ServiceAssignment TagId="911048693">
<Component Name="Registrar">
<ServiceId xmlns="urn:schema:Microsoft.Rtc.Management.Deploy.Topology.2008" SiteId="1" RoleName="Registrar" Instance="2" />
</Component>
<Component Name="PresenceFocus">
<ServiceId xmlns="urn:schema:Microsoft.Rtc.Management.Deploy.Topology.2008" SiteId="1" RoleName="UserServices" Instance="2" />
</Component>
</ServiceAssignment>
</ServiceAssignments>
</Value>
</Item>
</Dictionary>
</AnchoredXml>
i try this code but give me a null exception
var MyList = doc.Root.Elements("ServiceAssignment").Select(c=>c.Attribute(("TagId")).Value).ToList();
You have namespaced elements in the document so you need to include them in your queries.
XNamespace itemNs = "urn:schema:Microsoft.Rtc.Management.ScopeFramework.2008";
XNamespace assignmentNs = "urn:schema:Microsoft.Rtc.Management.Settings.ServiceAssignment.2008";
var query =
from item in doc.Descendants(itemNs + "Item")
from assignment in item.Descendants(assignmentNs + "ServiceAssignment")
select (long)assignment.Attribute("TagId");
string xmlString =
#"<AnchoredXml xmlns='urn:schema:Microsoft.Rtc.Management.ScopeFramework.2008' SchemaWriteVersion='1'>
<Key ScopeClass='Global'>
<SchemaId Namespace='urn:schema:Microsoft.Rtc.Management.Settings.ServiceAssignment.2008' ElementName='ServiceAssignments' />
<AuthorityId Class='Host' InstanceId='00000000-0000-0000-0000-000000000000' />
</Key>
<Dictionary Count='1'>
<Item>
<Key />
<Value Signature='2ffb6b0d-0239-4016-b08b-40520d1687ff'>
<ServiceAssignments xmlns='urn:schema:Microsoft.Rtc.Management.Settings.ServiceAssignment.2008'>
<ServiceAssignment TagId='659550892'>
<Component Name='Registrar'>
<ServiceId xmlns='urn:schema:Microsoft.Rtc.Management.Deploy.Topology.2008' SiteId='1' RoleName='Registrar' Instance='1' />
</Component>
<Component Name='PresenceFocus'>
<ServiceId xmlns='urn:schema:Microsoft.Rtc.Management.Deploy.Topology.2008' SiteId='1' RoleName='UserServices' Instance='1' />
</Component>
</ServiceAssignment>
<ServiceAssignment TagId='911048693'>
<Component Name='Registrar'>
<ServiceId xmlns='urn:schema:Microsoft.Rtc.Management.Deploy.Topology.2008' SiteId='1' RoleName='Registrar' Instance='2' />
</Component>
<Component Name='PresenceFocus'>
<ServiceId xmlns='urn:schema:Microsoft.Rtc.Management.Deploy.Topology.2008' SiteId='1' RoleName='UserServices' Instance='2' />
</Component>
</ServiceAssignment>
</ServiceAssignments>
</Value>
</Item>
</Dictionary>
</AnchoredXml>";
var doc = XDocument.Parse(xmlString);
var TagIds = doc.Descendants()
.Elements()
.Where(e =>
e.HasAttributes &&
e.Name.LocalName.Equals("ServiceAssignment") &&
e.Attribute("TagId") != null)
.Select(e => e.Attribute("TagId").Value);
Your XML contains namespaces, so you can't just compare the full name.
If you don't care about the namespaces, you can use XName.LocalName:
var result = from element in doc.Root.Descendants()
where element.Name.LocalName == "ServiceAssignment"
select (int)element.Attribute("TagId");
You can try with this code
var result = from item in XElement.Load("YourFile.xml").Root.Elements("ServiceAssignment")
where item.Attribute("TagId") == value
select item ;

Categories