Пример linq xml c sharp

C Sharp > Пример linq xml c sharp
09.10.2019 15:18:23


Наиболее часто встречающиеся слова в статье:

[XElement] [Element] [sABS_Code] [bSystemSource] [idrlSubjectABS] [idSubject] [ToString] [XDocument] ["sABS_Code"] [/sABS_Code]


Статья:

   class Pins
    {
        public string sABS_Code { get; set; }
        public string sPin { get; set; }
        public string bMain { get; set; }
        public string bSystemSource { get; set; }
        public string idrlSubjectABS { get; set; }
        public string idSubject { get; set; }
        public string idABS { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            string sString = @"<pins>
<pin>
<sABS_Code>ABS_SFAProd</sABS_Code>
<sPin>ABR-FW--ACCOUNT ACB</sPin>
<bMain>0</bMain><bSystemSource>1</bSystemSource>
<idrlSubjectABS>259705409</idrlSubjectABS><idSubject>218536614</idSubject><idABS>1461773</idABS></pin>
<pin>
<sABS_Code>ABS_SFAProd</sABS_Code>
<sPin>ABR-FW-</sPin>
<bMain>0</bMain><bSystemSource>1</bSystemSource>
<idrlSubjectABS>259705409</idrlSubjectABS><idSubject>218536614</idSubject><idABS>1461773</idABS></pin>
<pin>
<sABS_Code>ABS_SalesLogix</sABS_Code><sPin>A0CEWA00992Q</sPin><bMain>0</bMain><bSystemSource>1</bSystemSource><idrlSubjectABS>12359456</idrlSubjectABS><idSubject>6763494</idSubject><idABS>4187</idABS></pin></pins>
";
 
            // LINQ for XML
            XDocument xDoc = XDocument.Load(new StringReader(sString));
 
            var items = from xe in xDoc.Element("pins").Elements("pin")
                        where xe.Element("sABS_Code").Value == "ABS_EQ" || xe.Element("sABS_Code").Value == "ABS_SFAProd" || xe.Element("sABS_Code").Value == "ABS_SalesLogix"
                        select new Pins
                        {
                            // Name = xe.Attribute("name").Value,
                            sABS_Code = xe.Element("sABS_Code").Value,
                            sPin = xe.Element("sPin").Value,
                            bMain = xe.Element("bMain").Value,
                            bSystemSource = xe.Element("bSystemSource").Value,
                            idrlSubjectABS = xe.Element("idrlSubjectABS").Value,
                            idSubject = xe.Element("idSubject").Value,
                            idABS = xe.Element("idABS").Value
                        };
            // создаём новый xml документ
            XDocument xdoc = new XDocument();
 
            XElement pins = new XElement("pins");
            XElement pin_slx = new XElement("slx");
            XElement pin_eq = new XElement("eq");
            XElement pin_sfa = new XElement("sfa");
            foreach (var item in items)
            {
 
                if (item.sABS_Code == "ABS_SalesLogix")
                {
                    XElement sPin = new XElement("sPin", item.sPin);
                    pin_slx.Add(sPin);
                }
                if (item.sABS_Code == "ABS_EQ")
                {
                    XElement sPin = new XElement("sPin", item.sPin);
                    pin_eq.Add(sPin);
                }
                if (item.sABS_Code == "ABS_SFAProd")
                {
                    XElement sPin = new XElement("sPin", item.sPin);
                    pin_sfa.Add(sPin);
                }
 
            }
            pins.Add(pin_slx);
            pins.Add(pin_eq);
            pins.Add(pin_sfa);
            //корень
            xdoc.Add(pins);
            //сохраняем
            string result = xdoc.ToString();
 
 
            //вычищаем пробелы  и переводы  строк между  тегами
            var output = Regex.Replace(result, @">\s*<", "><", RegexOptions.Multiline);
            Console.Write(output);
            //Console.Write(xdoc.ToString().Replace(Environment.NewLine, String.Empty));
            //Console.Write(count_ABS_EQ.ToString()+"|"+ count_ABS_SFAProd.ToString() + "|" + count_ABS_SalesLogix.ToString());
            //xdoc.Save("phones.xml");
            Console.Read();
 
 
 
        }
    }