Find the attribute containing the ceratin string and changing the Value in C # using LINQ

I try to find the first attribute in an XML file that contains the string "name" (case insensitve) in it, and then change its value.

Here is an example of my xmls

//XML 1
<CtApproachTypes  
 DataclassId="1992A9CE-B048-4676-BFD4-FD81F1A65401"  
 EntityId="1992A9CE-B048-4676-BFD4-FD81F1A65401"  
 Name="PAR"  
 Remark="No Remarks"></CT_ApproachTypes> 

//XML 2
<MiMissions  
 DataclassId="C196A66B-4FA1-461C-9EEF-95A4F2085051"  
 EntityId="C196A66B-4FA1-461C-9EEF-95A4F2085051"  
 MissionName="Standard" 
 Visib="1"></MiMissions> 

//XML 3
<StSituations  
 DataclassId="679FAC3C-C9EF-41FD-9A13-957915605F01"  
 EntityId="679FAC3C-C9EF-41FD-9A13-957915605F01"  
 Sname="Standard"  
 Status="C"  
 Template="1"></StSituations> 

I want to be able to change the values ​​of "Name", "MissionName", "Sname" and print them on the console

EDIT here is my code

        public void updateXmlFile(string strFileName)
    {
        try
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(strFileName);

            string newValue = GetUniqueKey();

            XmlNodeList list = doc.SelectNodes("@*"); 
            IEnumerable<XmlNode> filteredList= list.Cast<XmlNode>().Where(item=>item.Value.ToLower().Contains("name"));

            foreach (XmlNode n in filteredList)
            {
                Console.WriteLine("NODES ARE : {0}", n);
            }
                doc.Save(strFileName);

        }
        catch (XmlException xex) { Console.WriteLine(xex); }

    }

This did not print anything, and I still need to change the original value using the string newValue

+3
source share
2 answers

XDocument, linq. System.Xml.Linq.

public void updateXmlFile(string strFileName)
{
    XDocument xDoc = XDocument.Load(strFileName);

    var nameAttributes = from el in xDoc.Root.Elements()
                         from attr in el.Attributes()
                         where attr.Name.ToString().ToLower().Contains("name")
                         select attr;

    foreach (var attribute in nameAttributes)
    {
        attribute.Value = "newValue";
        Console.WriteLine("{0}: {1}", attribute.Name, attribute.Value);
    }

    xDoc.Save(strFileName);
}

, XML , :

Name: newValue
MissionName: newValue
Sname: newValue
+2

, XML, LINQ, , "".

XmlDocument document = new XmlDocument();
...
XmlNodeList list = document.SelectNodes("@*");
IEnumerable<XmlNode> filteredList= list.Cast<XmlNode>().Where(item=>item.Value.ToLower().Contains("name"));

, , .

Edit:

, , . ;-) , .

    public void updateXmlFile(string strFileName)
    {
        try
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(strFileName);

            string newValue = GetUniqueKey();

            XmlNodeList list = doc.SelectNodes("//@*"); // Forgot the slashes here...
            IEnumerable<XmlNode> filteredList = list.Cast<XmlNode>().
                Where(item => item.Name.ToLower().Contains("name")); // Name property instead of Value

            foreach (XmlNode n in filteredList)
            {
                n.Value = newValue; // Setting the value.
                Console.WriteLine("FILTERED NODES ARE : {0}", n.Value);
            }

            doc.Save(strFileName);

        }
        catch (XmlException xex) { Console.WriteLine(xex); }

    }
+1

Source: https://habr.com/ru/post/1762613/


All Articles