Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[C#] xmlDocument (object) Durchsuchen mit xPath

Empfohlene Antworten

Veröffentlicht

Hallo ich habe folgende XML Struktur:

<?xml version="1.0" encoding="utf-8"?>

<VarDump xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<CreateDate>2006-02-07T11:46:18.0688198+01:00</CreateDate>

<ProgramVersion>1.1.1.1</ProgramVersion>

<SpsInfo>Write_SystemConfig</SpsInfo>

<Device>

<AdsNetId>0.0.0.0.0.0</AdsNetId>

<Struct Name="PRG_DOSINGSYSTEM.FB_DOSINGSYSTEM1">

<Var Name="I_FILEVERSION" Type="INT" Value="0" />

<Var Name="I_CONFIGURATORVERSION" Type="INT" Value="0" />

<Struct Name="FillMode">

<Var Name="I_FillVersion" Type="INT" Value="0" />

<Var Name="I_TagNam2" Type="INT" Value="0" />

</Struct>

</Struct>

<AdsPort>801</AdsPort>

</Device>

</VarDump>

diese habe ich mit xmlDocument eingelesen und alle Attribute der <Var>-Nodes in eine Arraylist geschrieben.

jetzt möchte ich im xlmDocument allerdings einen wert ändern dazu muss ich denn bestimmten tag suchen wie kann ich das mit xPath lösen bzw wie muss die syntax für den xPath string aussehen? zu beachten ist das die erste <struct> child structs besitzt!

so dachte ich es mal:

public void xmlDocSave()

{

foreach (VarTag vTag in VarTags){

string xPath = "Struct[attribute::Name=+vTag.StructName+"/Var [attribute::Name="+vTag.Name+]";

//string xPath = "VarDump/Device/Struct/Var";

XmlNode ChangeNode = this.xmlDoc.SelectSingleNode(xPath);

XmlAttribute ChangeAttribute = ChangeNode.Attributes["Value"];

ChangeAttribute.InnerText = Convert.ToString(vTag.Value);

}

}

ich benötige nur den richtigen xPath

Das ganze ist in C# ! hätt ich fast vergessen zu erwähnen

Gruß Nok

Naja hab es jetzt etwas hösslich gemacht!

wenn mir jemand allerdings sagen kann wie ich den xPath relative abfragen kann, fänd ich das echt nett ! :-)

public void xmlDocSave()

{

string xPath = null;

string strRootStructName=null;

XmlNode ChangeNode = null;

foreach (VarTag vTag in VarTags){

xPath = "VarDump/Device/Struct[@Name="+vTag.StructName+"]/Var[@Name="+vTag.Name+"]";

ChangeNode = this.xmlDoc.SelectSingleNode(xPath);

if (ChangeNode !=null){

strRootStructName = vTag.StructName;

}

else{

xPath = "VarDump/Device/Struct[@Name="+strRootStructName+"]/Struct[@Name="+vTag.StructName+"]/Var[@Name="+vTag.Name+"]";

ChangeNode = this.xmlDoc.SelectSingleNode(xPath);

}

XmlAttribute ChangeAttribute = ChangeNode.Attributes["Value"];

ChangeAttribute.InnerText = Convert.ToString(vTag.Value);

}

this.XMLWrite();

}

gruß nok

1. tags nicht in eine arraylist speichern...wenn du schon im xml namespace bist dann nutz auch:

xmlnodelist:

XmlNodeList myNodeList = myDocument.GetElementsByTagName(string xpath);

2. xmlelement myElem = myNodeList[int i];

auf dem xmlelement kannst du dann arbeiten und werte ändern

evtl. auch xmlnode notwendig

ansonsten:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemxml.asp

hier mal ein kleines snippet, ich nehme hier attribute des knotens, setze deren inhalt zusammen und erzeuge ein neues attribut das den wert bekommt:


static void erzeuge_KastenID(XmlDocument input, string ID_container)

        {

            #region IDs zuweisen

            XmlNodeList kasteList = input.GetElementsByTagName("kasten");

            for (int kastC = 0; kastC < kasteList.Count; kastC++)

            {

                XmlAttributeCollection kastColl = kasteList[kastC].Attributes;

                ID_container = kastColl["attr:pool"].Value + "-" + kastColl["attr:type"].Value + "-" + kastColl["attr:id"].Value + "-" + kastColl["attr:lang"].Value;

                XmlElement curKastElem = kasteList[kastC] as XmlElement;

                XmlAttribute kastID = input.CreateAttribute("ID");

                kastID.Value = ID_container;

                curKastElem.SetAttributeNode(kastID);

            }

            #endregion

        }

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.