I have been dealing with this xml document for a long time, and now it turns out that user should be able to see certain fields based on the permission level defined in the xml document. This is what the document looked previously:
<?xml version="1.0" encoding="utf-8" ?>
<AccessControl>
<Field>
<name>First_Name</name>
<label>First Name</label>
</Field>
<Field>
<name>Last_Name</name>
<label>Last Name</label>
</Field>
......
.....
</AccessControl>
This is how is parsed the document:
doc.Load(System.Web.Hosting.HostingEnvironment.MapPath("~/ConfigFile.xml"));
XmlNode root = doc.DocumentElement;
XmlNodeList xnList = root.SelectNodes("/AccessControl/Field");
foreach (XmlNode xn in xnList)
{
string fieldName = xn["name"].InnerText;
.....
....
}
Now I am adding a few permission nodes in the document, which will contain field nodes, similar to this:
<AccessControl>
<Permission Name = "permissionXYZ" >
<Field>
<name>First_Name</name>
<label>First Name</label>
</Field>
<Field>
<name>Last_Name</name>
<label>Last Name</label>
</Field>
....
</Permission>
<Permission Name = "permission123" >
<Field>
...
...
</Field>
</Permission>
</AccessControl>
How do I get only the required fields based on the permission defined in the document??
Using LINQ you could filter based on the attribute value and then select the Fields. Test.xml file in my example just holds your sample XML.
XDocument document = XDocument.Load("c:\\temp\\test.xml");
var fields = document.Descendants("Permission")
.Where(i => i.Attribute("Name") != null && i.Attribute("Name").Value == "permissionXYZ")
.Select(i => i.Descendants("Field"));
Related
Let's suppose i have the following Xml:
<Sections>
<Section>
<Item>
<Field> myfield </Field>
<Field> myfield </Field>
</Item>
<Item>
<Field> myfield </Field>
<Field> myfield </Field>
</Item>
</Section>
<Section>
<Item>
<Field> myfield </Field>
<Field> myfield </Field>
</Item>
</Section>
</Sections>
Now what i want is to loop though Sections, and work on each item separately, so i was thinking to do something like the following:
reader.ReadToDescendant("Section")
do
{
Console.WriteLine("Section");
reader.ReadToDescendant("Item");
do
{
var element = (XElement)XNode.ReadFrom(reader);
foreach (XElement el in element.Elements())
{
Console.WriteLine(el.Value);
}
}while(reader.ReadToNextSibling("Item"));
}while (reader.ReadToNextSibling("Section"))
My question is. If i repeat the same do-while loop for Item nodes, does the reader stop when it finds the closing Section tag or it will search in all the xml? Should i use reader.ReadSubtree() before the inner loop?
Note that i'm not looking for standard answer like "Use XDocument". I know that dom are easier to use, but they are not suitable for my situation
Use ReadSubtree to create inner reader to work with current node. Without this, the reader will not stop and continue the search until the end of the document.
reader.ReadToDescendant("Section");
do
{
Console.WriteLine("Section");
using (var innerReader = reader.ReadSubtree())
{
while (innerReader.ReadToFollowing("Field"))
{
Console.WriteLine("field");
}
}
} while (reader.ReadToNextSibling("Section"));
I have an xml document of type like this:
<?xml version="1.0" encoding="UTF-16"?>
<Recordset>
<Table>Recordset</Table>
<Rows>
<Row>
<Fields>
...
<Field>
<Alias>StatusName</Alias>
<Value>Scheduled</Value>
</Field>
<Field>
<Alias>U_Revision</Alias>
<Value>code00</Value>
</Field>
<Field>
<Alias>U_Quantity</Alias>
<Value>10.000000</Value>
</Field>
<Field>
<Alias>U_ActualQty</Alias>
<Value>0.000000</Value>
</Field>
...
</Fields>
</Row>
...
<Row>
<Fields>
...
<Field>
<Alias>StatusName</Alias>
<Value>Scheduled</Value>
</Field>
<Field>
<Alias>U_Revision</Alias>
<Value>code00</Value>
</Field>
<Field>
<Alias>U_Quantity</Alias>
<Value>150.000000</Value>
</Field>
<Field>
<Alias>U_ActualQty</Alias>
<Value>0.000000</Value>
</Field>
...
</Fields>
</Row>
</Rows>
</Recordset>
I have different values in field with alias of StatusName. There are some Scheduled, notScheduled, Realeased, Finished etc values. What I would like to do is to delete each node that contain node with alias StatusName and value lets say Scheduled or Finished.
I was thinking to do this more or less in that way however I am doing something wrong. May anybody let me on right way ?
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xml);
XmlNodeList nodes = xmlDocument.SelectNodes("//Rows[#StatusName='Finished']");
for (int i = nodes.Count - 1; i >= 0; i--)
{
nodes[i].ParentNode.RemoveChild(nodes[i]);
}
var newXml = nodes.ToString();
I would like to delete the whole node if contains with alias StatusName and specific value lets say Finished.
I would expect the result in new string variable.
I like to work with DataTable with xml, I found it very easy.
I used a DataTable to work with your nodes.
So, I took your xml file and wrote some code for you that might help you:
//READ THE XML FILE
XmlDocument xmlDoc = new XmlDocument();
//My path
xmlDoc.LoadXml(Properties.Resources.test);
//Read the xml file into a dataSet
DataSet ds = new DataSet();
XmlNodeReader xnr = new XmlNodeReader(xmlDoc);
ds.ReadXml(xnr);
//Your data will be store in the 4's dataTable of the dataSet ( the <field> )
for(int i=0;i<ds.Tables[4].Rows.Count;i++)
{
//Check the value as you wish
//Here i want to suppress all the <Field> nodes with <Value> = "Scheduled"
if ( ds.Tables[4].Rows[i]["Value"].ToString().Equals("Scheduled"))
{
//RemoteAt will remove all the node, so the node <Field> in your example data
ds.Tables[4].Rows.RemoveAt(i);
//If you want to only remove the node <Value> (and not all the <Field> node ) just do ds.Tables[4].Rows["Value"]=null;
}
}
//Write your new content in a new xml file
//As you wanted here you just read the new xml file created as a string
using (var stringWriter = new StringWriter())
using (var xmlTextWriter = XmlWriter.Create(stringWriter))
{
ds.WriteXml(xmlTextWriter);
xmlTextWriter.Flush();
stringWriter.GetStringBuilder().ToString();
//Here the result is in stringWriter, and there is 6 <Field> nodes, and not 8 like before the suppress
}
//If you want to create a new xml file with the new content just do
ds.WriteXml(yourPathOfXmlFile);
//( like rewriting the previous xml file )
I assume, you are going to delete entire <Row> which matches your condition
i.e.,
<Row>
<Fields>
...
<Field>
<Alias>StatusName</Alias>
<Value>Finished</Value>
</Field>
</Fields>
</Row>
The required XPath:
//Row[Fields[Field[Alias[text()='StatusName'] and Value[text() = 'Finished']]]]
C#
string xPath = #"//Row[Fields[Field[Alias[text()='StatusName'] and Value[text() = 'Finished']]]]";
var nodes = xmlDocument.SelectNodes(xPath);
for (int i = nodes.Count - 1; i >= 0; i--)
{
nodes[i].ParentNode.RemoveChild(nodes[i]);
}
var newXml = xmlDocument.OuterXml;
My C# code:
XDocument doc = XDocument.Load(filename);
IEnumerable<XElement> collection =
doc.Elements("BCIRequest").Elements("Card").Elements("SelectedPIN");
My XML document:
<?xml version="1.0" encoding="utf-8"?>
<BCIRequest Version="2.0"
xmlns="urn:xxxxxx:bci:request">
<Header>
<SenderCode>XX99</SenderCode>
<SenderID>9999</SenderID>
<SequenceNumber>123</SequenceNumber>
<CardGroupCount>2</CardGroupCount>
<CardCount>4</CardCount>
<BlockCount>2</BlockCount>
</Header>
<!--card groups (must precede cards and blocks)-->
<CardGroup RequestID="1">
<CustomerNumber>XX01234567</CustomerNumber>
<CardGroupName Emboss="true">GROUP ONE</CardGroupName>
</CardGroup>
<CardGroup RequestID="2"
RequestRef="87416CB7-DAEF-483A-BD08-1A885531D958">
<CustomerNumber>XX12345678</CustomerNumber>
<CardGroupName Emboss="false">GROUP TWO</CardGroupName>
</CardGroup>
<Card RequestID="3">
<CustomerNumber>XX01234567</CustomerNumber>
<DriverCard>
<Driver Emboss="true">MARGE SIMPSON</Driver>
</DriverCard>
<CardTypeID>10</CardTypeID>
<PurchaseCategoryID>11</PurchaseCategoryID>
<Reissue>false</Reissue>
<GeneratedPIN/>
<OdoPrompt>false</OdoPrompt>
<CRNPrompt>false</CRNPrompt>
</Card>
<Card RequestID="4">
<CustomerNumber>XX12345678</CustomerNumber>
<VehicleCard>
<VRN Emboss="true">KYI 830</VRN>
</VehicleCard>
<CardTypeID>10</CardTypeID>
<PurchaseCategoryID>11</PurchaseCategoryID>
<Reissue>false</Reissue>
<SelectedPIN>0123</SelectedPIN>
<OdoPrompt>false</OdoPrompt>
<CRNPrompt>false</CRNPrompt>
</Card>
<Card RequestID="5">
<CustomerNumber>XX01234567</CustomerNumber>
<BearerCard>
<Bearer Emboss="true">OPEN XXXXXX</Bearer>
</BearerCard>
<CardTypeID>10</CardTypeID>
<PurchaseCategoryID>11</PurchaseCategoryID>
<Reissue>false</Reissue>
<FleetPIN/>
<OdoPrompt>false</OdoPrompt>
<CRNPrompt>false</CRNPrompt>
</Card>
<Block RequestID="6">
<CustomerNumber>XX01234567</CustomerNumber>
<PAN>7002999999999999991</PAN>
</Block>
<Card RequestID="7"
RequestRef="956EA6C5-7D7E-4622-94D0-38CAD9FCC8DF">
<CustomerNumber>XX01234567</CustomerNumber>
<DriverCard>
<Driver Emboss="true">HOMER SIMPSON</Driver>
<VRN Emboss="true">795 DVI</VRN>
</DriverCard>
<EmbossText>SPRINGFIELD POWER</EmbossText>
<CardTypeID>10</CardTypeID>
<TokenTypeID>20</TokenTypeID>
<PurchaseCategoryID>30</PurchaseCategoryID>
<ExpiryDate>2018-12</ExpiryDate>
<Reissue>true</Reissue>
<SelectedPIN>0123</SelectedPIN>
<OdoPrompt>true</OdoPrompt>
<CRNPrompt>true</CRNPrompt>
<!--address with optional fields specified-->
<CardDeliveryAddress OneTimeUse="false">
<ContactName>M xxxx</ContactName>
<ContactTitle>Mr</ContactTitle>
<CompanyName>Sxxxx</CompanyName>
<Line1>Sector 22-F</Line1>
<Line2>Springfield Power Plant</Line2>
<Line3>xxx Road</Line3>
<City>xxxx</City>
<Zipcode>xxxx</Zipcode>
<CountryCode>xxx</CountryCode>
</CardDeliveryAddress>
<!--address with only required fields-->
<PINDeliveryAddress OneTimeUse="true">
<Line1>xxxx</Line1>
<City>xxx</City>
<Zipcode>xxxx</Zipcode>
<CountryCode>xxxx</CountryCode>
</PINDeliveryAddress>
<Limits>
<Value Transaction="unlimited" Daily="200" Weekly="unlimited" Monthly="400"/>
<Volume Transaction="100" Daily="unlimited" Weekly="unlimited" Monthly="unlimited"/>
<Transactions Daily="unlimited" Weekly="unlimited" Monthly="unlimited"/>
<Day Monday="true" Tuesday="true" Wednesday="true" Thursday="true" Friday="true" Saturday="false" Sunday="false"/>
<Time Start="unlimited" End="17:00:00"/>
</Limits>
<Products>
<FuelProductRestrictionID>40</FuelProductRestrictionID>
<NonFuelProductRestrictionID>51</NonFuelProductRestrictionID>
<NonFuelProductRestrictionID>52</NonFuelProductRestrictionID>
<NonFuelProductRestrictionID>53</NonFuelProductRestrictionID>
<NonFuelProductRestrictionID>54</NonFuelProductRestrictionID>
<NonFuelProductRestrictionID>55</NonFuelProductRestrictionID>
</Products>
</Card>
<Block RequestID="8"
RequestRef="69A3E44D-DC10-4BEE-9249-1FC3C651BA0E">
<CustomerNumber>xxxxx</CustomerNumber>
<PAN>xxxxxx</PAN>
</Block>
</BCIRequest>
I need to update the element value in the above values. The old value is:
<SelectedPIN>0123</SelectedPIN>
And the new value should be:
<SelectedPIN EncryptedPIN="TKDS" FormNumber="000793906306">****</SelectedPIN>
Can anyone can help me on this?
If I selected the BCIRequest element, it's returning a null value. I've tried many solutions but unable to get one working on this XML file.
There many ways an Xml can be be modified, I prefer XDocument
XDocument doc = XDocument.Parse(input);
foreach (var element in doc.Descendants("SelectedPIN")) // filter if you want single element, in example I modifed for all elements.
{
element.Add(new XAttribute("EncryptedPIN", "TKDS"));
element.Add(new XAttribute("FormNumber", "000793906306"));
element.Value = "xxxxx"; //new value
}
and finally you can save the document using
doc.Save();
Take a look at this Demo
The root node (BCIRequest) contains a namespace so you need to include that into your query. Something like this should work:
XNamespace ns = "urn:xxxxxx:bci:request";
IEnumerable<XElement> collection = doc.Elements(ns + "BCIRequest").Elements(ns + "Card").Elements(ns + "SelectedPIN");
I have the following XML doc... I have just left one "line" in for simplicity.
<?xml version="1.0" encoding="UTF-8"?>
<files>
<file type="INVOICES">
<document>blah.pdf</document>
<line>
<field name="JobNo">321654</field>
<field name="Issues">1</field>
<field name="PageCount">200</field>
<field name="PrintRun">250</field>
<field name="Size">Small</field>
</line>
</file>
</files>
C# code:
static void Main(string[] args)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("input.xml");
XmlNodeList itemNodes = xmlDoc.SelectNodes("//files/file/line");
foreach (XmlNode itemNode in itemNodes)
{
XmlNode jobNo = itemNode.SelectSingleNode("field");
if (jobNo != null)
Console.WriteLine(jobNo.InnerText);
}
Console.ReadKey();
}
This iterates through each line and displays the job number however I want to access the field by it's name JobNo i.e.
<field name="JobNo">321654</field> accessed with...
jobNo = itemNode.SelectSingleNode("JobNo");
I know I can change the xml but the XML is supplied by a customer so this is not really an option.
You can access that specific field element by providing the attribute name and value such as
XmlNode jobNo = itemNode.SelectSingleNode("field[#name='JobNo']");
I have this xml file
<config>
<PersonMapping>
<Field>
<Name>Id</Name>
<Position>0</Position>
</Field>
<Field>
<Name>FirstName</Name>
<Position>1</Position>
</Field>
<Field>
<Name>LastName</Name>
<Position>2</Position>
</Field>
<Field>
<Name insert='false' update='false'>Address1</Name>
<Position>3</Position>
</Field>
<Field>
<Name insert='false' update='false'>Address2</Name>
<Position>4</Position>
</Field>
</PersonMapping>
</config>
I have to create two collections based on settings in this file.
Depending on the users needs certain 'Field' tag may or may not have the 'insert' and 'update' attributes.
Insert collection will have all the tags which have insert = 'true' or not present
Update collection will have all the tags which have update = 'true' or not present
For the tags that don't have either of them they are true by default.
I wrote this query for insert
propertiesToInsertFromXML = from nameTag in xml.Element("Config").Element("PersonMapping").Elements("Field")
let insert = nameTag.Element("Name").Attribute("insert")
let update = nameTag.Element("Name").Attribute("update")
where insert == null || (bool)insert && update == null || (bool)update
select nameTag.Element("Name").Value;
Which gives Name, FirstName, LastName
Can someone help me here?
Regards.
Cast attribute to nullable boolean. For missing attributes you will get nulls. Then just verify if insert do not have value (attribute is missing) or if its value is true:
var insertTags = from name in xml.Descendants("Name")
let insert = (bool?)name.Attribute("insert")
where !insert.HasValue || insert.Value
select (string)name;
Remark: insert variable has type Nullable<bool> it is not of XAttribute type here.
Same with XPath:
var insertTags = xml.XPathSelectElements("//Name[#insert='true'or not(#insert)]")
.Select(n => (string)n);
You may try to change the where part to
where (insert == null || Convert.ToBoolean(insert.Value))
EDIT
Don't know if <Name insert='fasle' is desired for test or a typo.