简体   繁体   中英

Load XML file add Values and Save

I have an XML file that we are using systemlink for in AS400. I have the working XML file and if I just do a load document on the button click it writes to the database the hard coded values. However, I would like to patch values from textboxes in. Here is my XML:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE System-Link SYSTEM 'SystemLinkRequest.dtd'>
      <System-Link>
<Login userId='' password='' maxIdle='900000'
   properties='com.pjx.cas.domain.EnvironmentId=RN,
           com.pjx.cas.domain.SystemName=,
           com.pjx.cas.user.LanguageId=en'/>

<Request sessionHandle='*current' workHandle='*new'
   broker='EJB' maxIdle='1000'>

<Create name='newObject_ITItemLocation_Default' domainClass='EXT0149' retainResult='false'>
  <ApplyTemplate clientClass='EXT0149'>
    <![CDATA[Asset]]>
  </ApplyTemplate>
  <DomainEntity>
    <Property path='ponum'>
      <Value><![CDATA[P21851]]></Value>
    </Property>
    <Property path='itmnbr'>
      <Value><![CDATA[909520]]></Value>
    </Property>
    <Property path='itmcls'>
      <Value><![CDATA[1]]></Value>
    </Property>
    <Property path='itmloc'>
      <Value><![CDATA[1]]></Value>
    </Property>
    <Property path='srnum'>
      <Value><![CDATA[]]></Value>
    </Property>
  </DomainEntity>
</Create>

And here is my C#:

enterXmlDocument document = new XmlDocument();
document.Load(Server.MapPath("~/addNew.xml"));
XmlElement po = document.GetElementById("ponum");
po.Value = poTextBox.Text;
document.Save(Server.MapPath("~/addNew.xml")); 

Every time I try and run it I get an object not found so I'm guessing it can't find the ponum field. I'd like to patch the poTextBox.Text in where it is currently P21851. Any suggestions like I said if I just do the document.Load on button click it writes to the DB just fine, I just want to patch my values in.

Why dont you use serialization for converting the xml into an object so you can do what ever you want? Do you have the xsd?

Decided to take a different approach and just put it into a string and did an HttpWebRequest so that I could just patch in my values into my string. Thanks for the answers one day I'll mess with how to make that work in an XML document.

string asset = "<?xml version='1.0' encoding='UTF-8'?>" + "<!DOCTYPE System-Link SYSTEM 'SystemLinkRequest.dtd'>" + "<System-Link>" + "<Login userId='' password='' maxIdle='900000' properties='com.pjx.cas.domain.EnvironmentId=RN, com.pjx.cas.domain.SystemName=,com.pjx.cas.user.LanguageId=en'/>" + "<Request sessionHandle='*current' workHandle='*new' broker='EJB' maxIdle='1000'>" + "<Create name='newObject_ITItemLocation_Default' domainClass='EXT0149' retainResult='false'>" + "<ApplyTemplate clientClass='EXT0149'>" + "<![CDATA[Asset]]>" + "</ApplyTemplate>" + "<DomainEntity>" + "<Property path='ponum'><Value><![CDATA[" + poTextBox.Text + "]]></Value></Property>" + "<Property path='itmnbr'><Value><![CDATA[" + itemNoTextBox.Text + "]]></Value></Property>" + "<Property path='itmcls'><Value><![CDATA[" + classDropDown.SelectedValue +"]]></Value></Property>" + "<Property path='itmloc'><Value><![CDATA[" + locationDropDown.SelectedValue + "]]></Value></Property>" + "<Property path='srnum'><Value><![CDATA["+ serialTextBox.Text +"]]></Value></Property>" + "</DomainEntity>" + "</Create>" + "</Request>" + "</System-Link>";

String wRequestString = "SystemLinkRequest=" + HttpUtility.UrlEncode(asset);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM